版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FlyLikeButterfly/article/details/82909852
写文件Demo:
/**
* createtime : 2018年9月29日 上午9:55:12
*/
package filetest;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* TODO
* @author XWF
*/
public class WriteFileTest {
/**
* @param args
*/
public static void main(String[] args) {
WriteByFileOutputStream();
WriteByBufferedOutputStream();
WriteByFileWriter();
WriteByFiles();
WriteByBufferedWriter();
WriteByPrintWriter();
}
//FileOutputStream
public static void WriteByFileOutputStream() {
try(FileOutputStream fos = new FileOutputStream(new File("test1.txt"))){
fos.write("test\nhello\nworld\nEnd".getBytes());
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//BufferedOutputStream
public static void WriteByBufferedOutputStream() {
try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("test2.txt")))){
bos.write("this is\nBufferedOutputStream\ntest".getBytes());
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//FileWriter
public static void WriteByFileWriter() {
try(FileWriter fw = new FileWriter("test3.txt")){
fw.write("hello world,\nFileWriter test.\nThe end.");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//Files
public static void WriteByFiles() {
try {
Files.write(Paths.get("test4.txt"), "hellooo world\nhello worldddd".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
//BufferedWriter
public static void WriteByBufferedWriter() {
try(BufferedWriter bw = Files.newBufferedWriter(Paths.get("test5.txt"))){
bw.write("hello hello\nworld world.");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//PrintWriter
public static void WriteByPrintWriter() {
try(PrintWriter pw = new PrintWriter(new FileWriter("test6.txt"))){
pw.write("abcd\nefg\nhijk\nlmn\nopq\nrst\nuvwxyz");
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果:
读文件Demo:
/**
* createtime : 2018年9月29日 上午9:54:54
*/
package filetest;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* TODO
* @author XWF
*/
public class ReadFileTest {
/**
* @param args
*/
public static void main(String[] args) {
ReadByFileInputStream();
System.out.println("===================");
ReadByInputStreamReader();
System.out.println("===================");
ReadByBufferedReader();
System.out.println("===================");
ReadByBufferedInputStream();
}
//读字节
public static void ReadByFileInputStream() {
try(FileInputStream fis = new FileInputStream(new File("test1.txt"))){
int read;
while((read = fis.read()) != -1) {
System.out.println((char)read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try(FileInputStream fis = new FileInputStream(new File("test1.txt"))){
byte[] read = new byte[5];
while(fis.read(read) != -1) {
for(int i=0;i<read.length;i++) {
System.out.print((char)read[i]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//读字符
public static void ReadByInputStreamReader() {
try(InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("test2.txt")))){
int ch;
while((ch=isr.read()) != -1) {
System.out.println((char)ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try(InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("test2.txt")))){
char[] chs = new char[3];
while(isr.read(chs) != -1) {
System.out.print(chs);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//读整行
public static void ReadByBufferedReader() {
try(BufferedReader br = new BufferedReader(new FileReader(new File("test3.txt")))){
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void ReadByBufferedInputStream() {
try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test4.txt"))){
byte[] bytes = new byte[1024];
while(bis.read(bytes,0,1024) != -1) {
for(int i=0;i<bytes.length;i++) {
System.out.print((char)bytes[i]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果:
t
e
s
t
h
e
l
l
o
w
o
r
l
d
E
n
d
test
hello
world
End===================
t
h
i
s
i
s
B
u
f
f
e
r
e
d
O
u
t
p
u
t
S
t
r
e
a
m
t
e
s
t
this is
BufferedOutputStream
test===================
hello world,
FileWriter test.
The end.
===================
hellooo world
hello worldddd
参考:
http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
https://www.cnblogs.com/ll409546297/p/7197911.html