转自 http://blog.youkuaiyun.com/sunnyfans/article/details/7706700
Java中文件读写操作的作用是什么?
回答这个问题时应该先想到的是Java只是一门语言,我们的一种使用工具而已,这样答案就明晰了,就是将外来的各种数据写入到某一个文件中去,用以保存下来;或者从文件中将其数据读取出来,供我们使用。就如下电影过程,从网络资源中下载一部电影保存于你电脑中(写文件),当你想看的时候就用播放器打开(读文件)。
Java中如何对文件进行读写操作?
先理一理,Java中的流分两种,字节流和字符流,其中字节流的两个基类是InputStream和OutputStream;字符流的两个基类是Reader和Writer。所谓文件流,即我们对文件的操作留不开流。由此可知我们要用到某个类必然继承如上的四个基类之一。Java中一切都是类,一切都是对象。自然会想到文件操作有哪些类:
如下四个直接用到的类:
字节流中:FileInputStream和FileOutputStream
字符流中:FileReader和FileWriter
找到类就好办事了。剩下来的就是去找实现方法啦。
两种选择方案在这里,这就牵涉到我们如何选择合适的文件读写方式呢?
选择条件的区别:
以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
以字符为单位读取文件,常用于读文本,数字等类型的文件.
至于是否选择用Buffer来对文件输入输出流进行封装,就要看文件的大小,若是大文件的读写,则选择Buffer这个桶来提供文件读写效率。
如下是简单运用实例:
1、运用字节流对文件进行直接读写:
注:FileOutputStream(file, true);里面true参数表示不覆盖原文件,直接在文件后面追加添加内容。
public class FileTest
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
FileOutputStream out = new FileOutputStream(file, true);
String s = "Hello,world!\r\n";
out.write(s.getBytes());
out.flush();
out.close();
//FileInputStream in = new FileInputStream(file);
//byte [] b = new byte[20];
//in.read(b, 0, b.length);
//System.out.println(new String(b));
//in.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
2、运用字符流对文件进行直接读写:
public class File03
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter(file,true);
fw.write("Hello,world!\r\n");
fw.flush();
fw.close();
//FileReader fr = new FileReader(file);
//int i=0;
//String s ="";
//while( ( i = fr.read() )!= -1)
//{
// s = s +(char)i;
//}
//System.out.println(s);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
文件读写流用Buffer封装之后的运用:
1、对字节流封装后对文件进行读写:
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
// FileOutputStream out = new FileOutputStream(file, true);
// BufferedOutputStream bout = new BufferedOutputStream(out);
// String s = "I have a dream!";
// bout.write(s.getBytes());
// bout.flush();
// bout.close();
FileInputStream in = new FileInputStream(file);
BufferedInputStream bin = new BufferedInputStream(in);
byte[] b = new byte[15];
bin.read(b);
bin.close();
System.out.println(new String(b));
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
2、对字符流封装后对文件进行读写:
public class File03
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
// FileWriter fw = new FileWriter(file, true);
// BufferedWriter bw = new BufferedWriter(fw);
// String nextLine = System.getProperty("line.separator");
// bw.write("Hello,world!" + nextLine);
// bw.flush();
// bw.close();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int i = 0;
String s = "";
String temp = null;
while((temp=br.readLine())!=null)
{
s = s+temp;
}
System.out.println(s);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
Java文件操作:按行读取和写入
http://blog.youkuaiyun.com/gobitan/article/details/3716227
文件操作是任何语言中最为常用的部分,Java也不例外。这里主要介绍按行读取的文件操作和写入
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
publicclass JavaFile {
publicstaticvoid main(String[] args) {
try {
// read file content from file
StringBuffer sb= new StringBuffer("");
FileReader reader = new FileReader("c://test.txt");
BufferedReader br = new BufferedReader(reader);
String str = null;
while((str = br.readLine()) != null) {
sb.append(str+"/n");
System.out.println(str);
}
br.close();
reader.close();
// write string to file
FileWriter writer = new FileWriter("c://test2.txt");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(sb.toString());
bw.close();
writer.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
java IO 文件操作
http://blog.youkuaiyun.com/yue7603835/article/details/6678655
/*
java 文件操作
java中文件操作的类是 File类 可以实现对文件的创建删除 修改等
File类 在 java.io包中
下面是 通过 java.io.File类实现对文件操作
File类没有提供对文件的读写方法
*/
import java.io.*; //导入io包
class Test
{
public static void main(String []args) throws Exception //直接吧所有异常抛出给 JVM虚拟机
{
//一个File类对象代表一个文件或者目录
//File.seperator 表示 当前盘符与系统相关的抽象目录 或者一个\ /
File dir=new File(File.separator +"newDir"); //在当前路径下创建一个目录
dir.mkdir(); //创建目录
File file1=new File(dir,"1.txt"); //在 newDir目录下创建1.txt这是 构造函数的重载形式 dir是父目录
file1.createNewFile() ;//在dir目录下创建一个新文件
File file2=new File("d:\\2.txt"); //直接用绝对路径创建
file2.createNewFile(); //创建文件
/*在运行的时候生成文件在推出的时候删除文件*/
/*File fRun=new File("d:\\fRun.txt");
fRun.createNewFile() ; //create
fRun.deleteOnExit() ;//这个文件会在程序推出的时候删除掉
Thread.sleep(3000);
*/
/* java创建临时文件 并且删除临时文件 */
int count =0;
while(count++<10)
{
File ftemp=File.createTempFile("xiaowei",".tmp") ; //创建临时文件 File类静态方法
ftemp.deleteOnExit();//在退出程序的时候删除临时文件
//临时文件目录在 系统变量的temp
}
/*
查看一个目录下所有文件个目录 并且进行过滤
*/
File fileter=new File(File.separator); //代表当前目录 我是D盘
String []name=fileter.list();//获得当前目录下的多有文件目录名字
System.out.println("未加滤镜");
for(int i=0;i<name.length;i++)
System.out.println(name[i]);
System.out.println("\n\n加滤后");
String []fname=fileter.list(new FilenameFilter() //这里传递的是一个匿名的内部类因为接口不能直接实例对象
{
public boolean accept(File dir, String name)
{
return name.indexOf("txt")!=-1;//不是txt返回 false 是txt返回 true
}
}
) ;//通过滤镜获得文件目录名 传递一个 FilenameFilter接口实现对象 这里是匿名
for(int i=0;i<fname.length;i++)
System.out.println(fname[i]);
}
}