您的位置:首页 > 编程语言 > Java开发

java写文件

2016-06-24 10:57 218 查看
1 使用FileWrite写文本文件
2
3
4 public static void useFileWriter(String fileName) throws IOException {
5 File file = new File(fileName);
6 FileWriter fileWriter = new FileWriter(file);
7
8 fileWriter.write("it is a test");
9
10 fileWriter.close();
11 }
12
13
14
15 使用BufferedWrite写文本文件
16
17
18 public static void useBufferedWriter(String fileName) throws IOException{
19 File file = new File(fileName);
20 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));
21
22 bufferedWriter.write("hello bufferedwrite");
23
24 bufferedWriter.flush();
25 bufferedWriter.close();
26 }
27
28
29
30 使用Files写文件,最简单
31
32
33 public static void useJdk8(String fileName) throws IOException {
34 Files.write(Paths.get(fileName), "hello usejdk8".getBytes(), StandardOpenOption.CREATE);
35 }
36
37
38 私用FileOutputStream写文件
39
40
41 public static void useFileOutputStream(String fileName) throws IOException{
42 File file = new File(fileName);
43
44 FileOutputStream fileOutputStream = new FileOutputStream(file);
45 fileOutputStream.write("hello fileoutputstream".getBytes());
46
47 fileOutputStream.flush();
48
49 fileOutputStream.close();
50 }
51
52
53
54 使用BufferedFileOutputStream写文件,速度最快,数据cache在jvm中,容易丢数据
55
56
57 public static void useBufferedFileOutputStream(String fileName) {
58 File file = new File(fileName);
59
60
61 BufferedOutputStream bufferedOutputStream = null;
62 try {
63 bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
64
65 bufferedOutputStream.write("hello BufferedFileOutputStream".getBytes());
66
67 bufferedOutputStream.flush();
68 }catch(IOException e) {
69
70
71 }finally {
72 if(bufferedOutputStream!=null) {
73 try {
74 bufferedOutputStream.close();
75 } catch (IOException e1) {
76 //TODO do something
77 }
78 }
79 }
80
81 }
82
83
84
85 使用RandomAccessFile写文件,速度最慢,直接刷盘
86
87
88 public static void useRandomAccessFile(String fileName) {
89 RandomAccessFile randomAccessFile = null;
90
91
92 try {
93 randomAccessFile = new RandomAccessFile(fileName, "rw");
94 randomAccessFile.seek(15); //从第15个byte位置开始写, 原文件的第15个之后的字符会被覆盖一部分
95 randomAccessFile.write("useRandomAccessFile".getBytes());
96 } catch (IOException e) {
97 e.printStackTrace();
98 }finally {
99 if(randomAccessFile!=null) {
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

使用FileChannel写文件

public static void useFileChannel(String fileName) {

FileChannel fileChannel = null;
try {
FileChannel channel = new FileOutputStream(fileName).getChannel();
channel.write(ByteBuffer.wrap("useFileChannel".getBytes()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileChannel!=null) {
try {
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

使用MappedByteBuffer写文件,速度快,OS级别内存映射

public static void useMappedByteBuffer(String fileName){

RandomAccessFile randomAccessFile = null;

try {
randomAccessFile = new RandomAccessFile(fileName, "rw");
FileChannel fileChannel = randomAccessFile.getChannel();

String content = "useMappedByteBuffer";

MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, content.getBytes().length);

mappedByteBuffer.put(content.getBytes()); //mappedByteBuffer大小不能小于content的字节数

} catch (IOException e) {
e.printStackTrace();
}finally {
if(randomAccessFile!=null) {
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: