欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java文件读取和写入(文件读写java代码)

程序员文章站 2022-04-10 18:45:01
java操作文件在经常会使用到,本文汇总了部分java操作文件的读取常用工具类,希望可以帮到大家。直接上代码。一、读取文件成字节将文件内容转为字节,需要使用到fileinputstream文件字节输入...

java操作文件在经常会使用到,本文汇总了部分java操作文件的读取常用工具类,希望可以帮到大家。直接上代码。

一、读取文件成字节

将文件内容转为字节,需要使用到fileinputstream文件字节输入流,将文件输入到文件字节输入流中,使用fileinputstream的available()方法获取与之关联的文件的字节数,然后使用read()方法读取数据,最后记得关闭文件字节流即可。

//读取文件成字节数组
  public static byte[] file2byte(string path){
        try {
            fileinputstream in =new fileinputstream(new file(path));
            byte[] data=new byte[in.available()];
            in.read(data);
            in.close();
            return data;
        } catch (exception e) {
            e.printstacktrace();
            return null;
        }
    }

二、将字节写入文件

与一中的读取文件成字节类似,字节写入文件使用fileoutputstream流,即可将字节写入到文件中。调用fileoutputstream的write()方法,写入数据,之后关流。

 //将字节数组写入文件
  public static void byte2file(string path,byte[] data) {
        try {
            fileoutputstream outputstream  =new fileoutputstream(new file(path));
            outputstream.write(data);
            outputstream.close();
        } catch (exception e) {
            e.printstacktrace();
        }
    }

三、按行读取文件成list

经常遇到需要将一个文档中的文本按行输出,这是我们可以使用bufferedreader 和 inputstreamreader流处理。具体代码如下。

 //按行读取文件成list
  public static arraylist<string> file2list(string path,string encoder) {
        arraylist<string> alline=new arraylist<string>();
        try {
            bufferedreader in =new bufferedreader(new inputstreamreader(new fileinputstream(path),encoder));
            string str=new string();
            while ((str=in.readline())!=null) {
                alline.add(str);
            }
            in.close();
        } catch (exception e) {
            e.printstacktrace();
        }
        return alline;
    }

四、输出list到文件

 //输出list到文件
  public static void list2file(string path,arraylist<string> data,string encoder) {
        try {
            bufferedwriter out =new bufferedwriter(new outputstreamwriter(new fileoutputstream(path),encoder));
            for (string str:data) {
                out.write(str);
                out.newline();
            }
            out.flush();
            out.close();
        } catch (exception e) {
            e.printstacktrace();
        }
    }

五、从标准输入中读入

  //从标准输入中读入
  public static string system2str() throws ioexception{
        bufferedreader stdin=new bufferedreader(new inputstreamreader(system.in));
        return stdin.readline();
    }

六、读取文件成字符串

//读取文件成字符串
  public static string file2str(string path,string encoder){
        stringbuilder sb=new stringbuilder();
        try {
            bufferedreader in =new bufferedreader(new inputstreamreader(new fileinputstream(path),encoder));
            string str=new string();
            while ((str=in.readline())!=null) {
                sb.append(str);
            }
            in.close(); 
        } catch (exception e) {
            e.printstacktrace();
        }
        return sb.tostring();
    }

七、输出字符串到文件

 //输出字符串到文件
  public static void str2file(string path,string data,string encoder){
        try {
            bufferedwriter out =new bufferedwriter(new outputstreamwriter(new fileoutputstream(path),encoder));
            out.write(data);
            out.flush();
            out.close();
        } catch (exception e) {
            e.printstacktrace();
        }
    }

八、读取文件成数据矩阵

//读取文件成数据矩阵
  public static arraylist<double> file2matrix(string path){
        arraylist<double> alldata=new arraylist<double>();
        try {
            datainputstream in=new datainputstream(new bufferedinputstream(new fileinputstream(path)));
            //利用datainputstream来读数据
            while(true)
            {
                alldata.add(in.readdouble());
            }
        } catch (exception e) {
            e.printstacktrace();
        }
        return alldata;
    }

总结

对文件的操作方式还有很多,本文使用的只是一个基础的参考示例,欢迎学习交流。感谢阅读。