我们先来介绍一下字节流和字符流的概念及区别:
(学习视频分享:java教学视频)
区别字节流和字符流概念
字节流:字节流读取的时候,读到一个字节就返回一个字节;主要用于读取图片,mp3,avi视频文件。
字符流:字符流使用了字节流读到一个或多个字节,如读取中文时,就会一次读取2个字节。只要是处理纯文本数据,就要优先考虑使用字符流。
字节流和字符流区别
字节流操作的基本单元为字节;字符流操作的基本单元为unicode码元。字节流默认不使用缓冲区;字符流使用缓冲区。字节流通常用于处理二进制数据,实际上它可以处理任意类型的数据,但它不支持直接写入或读取unicode码元;字符流通常处理文本数据,它支持写入及读取unicode码元。
文件常用操作:
创建、删除文件夹
string path = "f:\\test";file myfile = new file(path);if (!myfile.exists()) { // 创建文件夹 myfile.mkdir(); // myfile.mkdirs(); // 删除文件夹 myfile.delete();}// mkdirs()可以建立多级文件夹, mkdir()只会建立一级的文件夹
创建、删除文件
string content = "hello world";// 第一种方法:根据文件路径和文件名string path = "f:\\test";string filename = "test.txt";file myfile = new file(path,filename);// 第二种方法string file = "f:\\test\\test.txt";file myfile = new file(file);if (!myfile.exists()) { // 创建文件(前提是目录已存在,若不在,需新建目录即文件夹) myfile.createnewfile(); // 删除文件 myfile.delete();}
写入文件
// 第一种:字节流fileoutputstreamfileoutputstream fop = new fileoutputstream(myfile); byte[] contentinbytes = content.getbytes();fop.write(contentinbytes); fop.flush(); fop.close(); // 第二种:filewriter(参数true为追加内容,若无则是覆盖内容)filewriter fw = new filewriter(myfile,true);fw.write(content);fw.close();// 第三种:bufferedwriterbufferedwriter bw = new bufferedwriter(new filewriter(myfile,true));bw.write(content); bw.flush(); bw.close(); // 第四种:打印流printstream和printwriter// 字节打印流:printstream// 字符打印流:printwriterprintwriter pw = new printwriter(new filewriter(myfile,true)); pw.println(content); // 换行pw.print(content); // 不换行pw.close();// 常用bufferedwriter和printwriter
读取文件
fileinputstream
// 第一种:以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 inputstream in = new fileinputstream(myfile);// 一次读一个字节int tempbyte; while ((tempbyte = in.read()) != -1) { system.out.write(tempbyte); } in.close();// 一次读多个字节int byteread = 0;byte[] tempbytes = new byte[100];readfromfile.showavailablebytes(in);while ((byteread = in.read(tempbytes)) != -1) { system.out.write(tempbytes, 0, byteread); } // system.out.write()方法是字符流,system.out.println()方法是字节流
inputstreamreader
// 第二种:以字符为单位读取文件,常用于读文本,数字等类型的文件 reader reader = new inputstreamreader(new fileinputstream(myfile)); // 一次读一个字节int tempchar; while ((tempchar = reader.read()) != -1) { // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 if (((char) tempchar) != '\r') { system.out.print((char) tempchar); } } reader.close();// 一次读多个字节char[] tempchars = new char[30]; int charread = 0; // 读入多个字符到字符数组中,charread为一次读取字符数 while ((charread = reader.read(tempchars)) != -1) { // 同样屏蔽掉\r不显示 if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r')) { system.out.print(tempchars); } else { for (int i = 0; i < charread; i++) { if (tempchars[i] == '\r') { continue; } else { system.out.print(tempchars[i]); } } } }
bufferedreader
// 第三种:以行为单位读取文件,常用于读面向行的格式化文件 bufferedreader reader = new bufferedreader(new filereader(myfile));string tempstring = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempstring = reader.readline()) != null) { // 显示行号 system.out.println("line " + line + ": " + tempstring); line++; } reader.close(); // 常用bufferedreader
遍历文件(以删除一个文件夹下所有文件为例)
file[] files=myfile.listfiles(); for(int i=0;i<files.length;i++){ if(files[i].isdirectory()){ files[i].delete(); } }
文件函数:
//判断文件是否存在 myfile.exists()//读取文件名称 myfile.getname()//读取文件路径(相对路径) myfile.getpath()//读取文件绝对路径 myfile.getabsolutepath()//读取文件的父级路径 new file(myfile.getabsolutepath()).getparent()//读取文件的大小 myfile.length()//判断文件是否被隐藏 myfile.ishidden()//判断文件是否可读 myfile.canread()//判断文件是否可写 myfile.canwrite()//判断文件是否为文件夹 myfile.isdirectory()
相关推荐:java入门教程
以上就是java中常用的文件操作有哪些的详细内容。