首页 > 作文

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

更新时间:2023-04-04 06:02:22 阅读: 评论:0

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

一、读取文件成字节

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

甘肃省艺术学校
//读取文件成字节数组  public static byte[] file2byte(string path){        try {            fileinputstream in =new fileinputs工艺品加工厂tream(new file(path));            byte[] data=new byte[in.available()];            in.read(data);            in.clo();            return data;        } catch (exception e) {            e.printstacktrac安全生产检查记录e();            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.clo();        } 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.clo();        } 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猜一成语.clo();        } 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.clo();         } 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.clo();        } 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;    }

总结

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

本文发布于:2023-04-04 06:02:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/1e2312d27074776a6544c25cd9a67ab3.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:java文件读取和写入(文件读写java代码).doc

本文 PDF 下载地址:java文件读取和写入(文件读写java代码).pdf

标签:文件   字节   字符串   成数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图