java的文件读写

1.这是第一篇文章,写的不错,你须要逐字逐句的去看,会发现分清楚字节流和字符流就好多了,而后咱们须要看第二篇博客,如何相互转化。 html

第三篇是个例子,写的通常,不怎么样,能够参考,看看。 网络

http://blog.sina.com.cn/s/blog_4a9f789a0100ik3p.html
工具

Java中文件读写操做的做用是什么? spa


回答这个问题时应该先想到的是Java只是一门语言,咱们的一种使用工具而已,这样答案就明晰了,就是将外来的各类数据写入到某一个文件中去,用以保存下来;或者从文件中将其数据读取出来,供咱们使用。就以下电影过程,从网络资源中下载一部电影保存于你电脑中(写文件),当你想看的时候就用播放器打开(读文件)。

Java中如何对文件进行读写操做?

先理一理,Java中的流分两种,字节流和字符流,其中字节流的两个基类是InputStream和OutputStream;字符流的两个基类是Reader和Writer。所谓文件流,即咱们对文件的操做留不开流。由此可知咱们要用到某个类必然继承如上的四个基类之一。Java中一切都是类,一切都是对象。天然会想到文件操做有哪些类:
以下四个直接用到的类:
字节流中:File InputStream和FileOutputStream
字符流中:FileReader和FileWriter
找到类就好办事了。剩下来的就是去找实现方法啦。

两种选择方案在这里,这就牵涉到咱们如何选择合适的文件读写方式呢?
选择条件的区别:
以字节为单位读取文件,经常使用于读二进制文件,如图片、声音、影像等文件。
以字符为单位读取文件,经常使用于读文本,数字等类型的文件.
至因而否选择用Buffer来对文件输入输出流进行封装,就要看文件的大小,如果大文件的读写,则选择Buffer这个桶来提供文件读写效率。

以下是简单运用实例:
一、运用字节流对文件进行直接读写:
注: 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();
}
}
}
二、运用字符流对文件进行直接读写:
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封装以后的运用:
一、 对字节流封装后对文件进行读写:
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();
}
}
}
二、 对字符流封装后对文件进行读写:
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();
}
}
}
相关文章
相关标签/搜索