1.字节流操作
字节流操作的输入/输出。这里介绍2个类:
输入:FileInputStream
输出:FileOutputStream
1.1 FileInputStream
读入的文件内容如图:
代码如下:
import java.io.*;
public class FileInput {
public static void main(String[] args){
try{
FileInputStream file = new FileInputStream("/Users/xxm/Documents/soft/helloSpark.txt");
while(file.available() > 0){
System.out.println((char)file.read());
}
file.close();
}
catch(Exception e){
System.out.println("not found file");
}
}
}
运行结果:
H
e
l
l
o
S
p
a
r
k
H
e
l
l
o
H
a
d
o
o
p
H
e
l
l
o
F
l
i
n
k
S
p
a
r
k
i
s
A
w
e
s
o
m
e
1.2 FileOutputStream
实现功能:把上面出现的文件helloSpark.txt中的内容复制到新的文件write.txt里,并加入自己的一些字节。
代码如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutput {
public static void main(String[] args){
int i;
try{
FileInputStream aa = new FileInputStream("/Users/xxm/Documents/soft/helloSpark.txt");
FileOutputStream bb = new FileOutputStream("/Users/xxm/Documents/soft/write.txt");
bb.write('h');
bb.write('e');
bb.write('l');
bb.write('l');
bb.write('o');
bb.write('\n');
byte[] writetest ={'w','r','i','t','e',' ','f','r','o','m',' ','m','y',' ','f','i','l','e'};
bb.write(writetest);
bb.write('\n');
i = aa.read();
while(i != -1){
bb.write(i);
i = aa.read();
}
aa.close();
bb.close();
}
catch(IOException e){
System.out.println("do not open this file");
}
}
}
运行结果,在文件中打开write.txt查看内容。
1.3 DataInputStream和DataOutputStream用法
使用的类DataInputStream(Obj)和DataOutputStream(Obj)来读写文件。
import java.io.*;
public class DataOne {
public static void main(String[] args) {
try{
FileOutputStream file1 = new FileOutputStream("/Users/xxm/Documents/soft/do.txt");
DataOutputStream da = new DataOutputStream(file1);
da.writeInt(12);
da.writeDouble(12.12d);
da.writeBoolean(true);
da.writeChars("Java数据流#");
file1.close();
FileInputStream file2 = new FileInputStream("/Users/xxm/Documents/soft/do.txt");
DataInputStream di = new DataInputStream(file2);
System.out.println(di.readInt());
System.out.println(di.readDouble());
System.out.println(di.readBoolean());
char aa;
while((aa = di.readChar()) != '#')
System.out.print(aa);
System.out.println("\n");
file2.close();
}
catch(IOException e){
System.out.println(e);
}
}
}
运行结果
12
12.12
true
Java数据流
1.4 用缓冲流读写
一个个字节读写速度太慢,不能满足要求,可以使用缓冲流来读写。
使用的类BufferedInputStream(Obj)和BufferedOutputStream(Obj)来读写文件。
import java.io.*;
public class DataTwo {
public static void main(String[] args){
FileInputStream fis;
FileOutputStream fos;
BufferedInputStream bis;
BufferedOutputStream bos;
int i;
try{
fis = new FileInputStream("/Users/xxm/Documents/soft/one.txt");
bis = new BufferedInputStream(fis);
fos = new FileOutputStream("/Users/xxm/Documents/soft/two.txt");
bos = new BufferedOutputStream(fos);
i = bis.read();
while(i!=-1){
bos.write(i);
bos.flush();
i = bis.read();
}
fis.close();
fos.close();
bis.close();
bos.close();
}
catch(IOException e){
System.out.println(e);
}
}
}
运行结果:
2.字符流操作
2.1 如何读写字符文件
用FileReader和FileWriter类进行读写字符文件。
2.1.1 FileReader
import java.io.*;
public class ReaderOne {
public static void main(String[] args){
try{
FileReader fr = new FileReader("/Users/xxm/Documents/soft/helloSpark.txt");
char[] c = new char[500];
while(fr.read(c) != -1){
System.out.print(new String(c));
fr.close();
}
}
catch(Exception e){
System.out.println(e);
}
}
}
运行结果:
Hello Spark
Hello Hadoop
Hello Flink
Spark is Awesomejava.io.IOException: Stream closed
这里运行出了问题java.io.IOException: Stream closed。修改了几次,每成功,先放放。有大神看到,可以留言问题在哪里?
2.1.2 通过字符读取文件
import java.io.BufferedReader;
import java.io.FileReader;
public class BufRW {
public static void main(String[] args){
try{
FileReader fr = new FileReader("/Users/xxm/Documents/soft/helloSpark.txt");
BufferedReader br = new BufferedReader(fr);
String a;
while((a = br.readLine()) != null){
System.out.println(a);
}
fr.close();
br.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
运行结果:
Hello Spark
Hello Hadoop
Hello Flink
Spark is Awesome
2.1.2 通过字符读取文件及写入
package com.xxm.javaLange.test;
import java.io.*;
public class BufRW2 {
public static void main(String[] args){
try{
FileInputStream fis = new FileInputStream("/Users/xxm/Documents/soft/two.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream("/Users/xxm/Documents/soft/one.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
String s;
while((s = br.readLine()) != null){
bw.write(s);
bw.flush();
}
br.close();
bw.close();
}
catch(IOException e){
System.out.println(e);
}
}
}
3.常用I/o处理文件
举个例子:
import java.io.*
;
public class FileDemo {
public static void main(String[] args){
File f2 = new File("/Users/xxm/Documents/soft/one.txt");
//File f2 = new File(f1,"/Users/xxm/Documents/soft/six.txt");
System.out.println(f2);
System.out.println(f2.exists());
System.out.println(f2.isFile());
System.out.println(f2.isDirectory());
System.out.println(f2.canRead());
System.out.println(f2.canWrite());
System.out.println(f2.isHidden());
System.out.println(f2.setReadOnly());
System.out.println(f2.getName());
System.out.println(f2.getParentFile());
System.out.println(f2.getAbsolutePath());
System.out.println(f2.lastModified());
System.out.println(f2.length());
System.out.println(f2.mkdir());
}
}
运行结果:
/Users/xxm/Documents/soft/one.txt
true
true
false
true
true
false
true
one.txt
/Users/xxm/Documents/soft
/Users/xxm/Documents/soft/one.txt
1457870343000
17
false
在使用中FileWriter的调试一直报错。不知是不是参考的书籍有误。所以找来博客中其他人的文章。作为参考
java中的 FileWriter类 和 FileReader类的一些基本用法
http://blog.csdn.net/liuhenghui5201/article/details/8276278