栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java从文件读取String对象与将String对象写入文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java从文件读取String对象与将String对象写入文件

注*:这里说的输入输出是针对于java项目来说的,输入指的是从外部文件获取内容输入到java类型中,输出指的是从java类型输出到外部文件。

一、输出:从java对象输出到文件中:

java对象可以是String、StringBuffer、StringBuilder类型等,文件的格式可以是.txt/.doc/.docx格式等。

1. 通过FileWriter类型:
public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("template/output.txt");
		fw.write("java");
		fw.close();
}
2. 通过BufferedWriter类型:
public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("template/output.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        //使用缓冲区中的方法将数据写入到缓冲区中。
        bw.write("hello world !");
        bw.newline();
        bw.newline();
        bw.write("!hello world !");
        bw.write("!hello world !");
        //使用缓冲区中的方法,将数据刷新到目的地文件中去。
        bw.flush();
        //关闭缓冲区,同时关闭了fw流对象
        bw.close();
}
3. 根据BufferedOutputStream( 字节缓冲输入流)类型:
public class Output {
    public static void main(String[] args) throws Exception {
        //符合Java一种设计模式:装饰者设计模式(过滤器:Filter)
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("template/output.txt")) ;
        //写数据
        bos.write("hello".getBytes());
        //释放资源
        bos.close();
    }
}
二、输入:读取文件,转换成java对象:

文件的格式可以是.txt/.doc/.docx格式等,可以转换成String、StringBuffer、StringBuilder类型等。

1. 通过BufferedReader类型:
import java.io.*;
class FileToString{
    public static void main(String[] args) throws IOException{
        String s = new String();
        StringBuffer sb = new StringBuffer();
        BufferedReader in = new BufferedReader(
            new FileReader("c:\j2sdk\stream\3.doc")
        );

        try {
            while((s = in.readLine()) != null) sb.append( s+"nr");
            in.close();
            System.out.println(sb);
        }
        catch(IOException e){
            System.out.println("file error");
        }
    }
}
2. 根据BufferedInputStream( 字节缓冲输入流)类型:
public class FileToString {
    public static void main(String[] args) {
        BufferedInputStream bufferedInput = null;
        byte[] buffer = new byte[1024];
        try {
            bufferedInput = new BufferedInputStream(new FileInputStream("template/test.txt"));
            int bytesRead = 0;
            //从文件中按字节读取内容,到文件尾部时read方法将返回-1
            while ((bytesRead = bufferedInput.read(buffer)) != -1) {
                //将读取的字节转为字符串对象
                String chunk = new String(buffer, 0, bytesRead);
                System.out.print(chunk);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {//关闭 BufferedInputStream
                if (bufferedInput != null) bufferedInput.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

可以根据自己的项目进行改写,StringBuffer转成String的方法:

StringBuffer sb = "java";
return sb.toString();

(错误代码:)

class FileToString{
    public static void main(String[] args) throws IOException{
        String s = new String();
        StringBuffer sb = new StringBuffer();
        BufferedInputStream in = new BufferedInputStream(
                new FileInputstream("c:\j2sdk\stream\3.doc")
        );

        try {
            while((s = in.readLine()) != null) sb.append( s+"nr");
            in.close();
            System.out.println(sb);
        }
        catch(IOException e){
            System.out.println("file error");
        }
    }
}

 字节缓冲输出流

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号