java写文件
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();
}
}
}
}
java写文件的更多相关文章
- java写文件实现换行
Java 写文件实现换行 第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exist ...
- java写文件时,输出不完整的原因以及解决方法
在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bo=new BufferedWriter(new FileWriter("sql语句.txt")) ...
- java 写文件解析
import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...
- java写文件时,输出不完整的原因以及解决方法close()或flush()
在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bw=new BufferedWriter(new FileWriter("sql语句.txt")) ...
- java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结
代码如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; public ...
- java写文件读写操作(IO流,字符流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
- java写文件读写操作(IO流,字节流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
- java写文件的基本操作
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOExce ...
- Java 写文件实现换行
第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exists()) file.creat ...
随机推荐
- PHP 弹出文件下载
/** * @author default7<default7@zbphp.com> * @description 演示PHP弹出下载的原理 * * @param $file_name * ...
- 单点登录(SSO)系统的总结
前些天一位其他开发部门的同事找到我们了解一些关于SSO单点登录的事,他们要做单点登录,同时也需要和我们这边的系统做集成,要我帮忙做一单点登录,了解关于单点登录的解决方案和资料,虽然做单点登录已经很久了 ...
- super的作用(转自百度)
super出现在继承了父类的子类中.有三种存在方式:第一种super.xxx;(xxx为变量名或对象名)这种方法意义为,获取父类中的名字为xxx的变量或方法引用.使用这种方法可以直接访问父类中的变量或 ...
- webapi输入验证过滤器ValidationActionFilter
public class validationActionFilter:ActionFilterAttribute { public override void OnActionExecuting(S ...
- Lua 之os库
标准os库 os.rename(oldname, newname) 文件重命名: os.remove(filename) 删除一个文件 os.execute(cmd) os.execute可运行一条系 ...
- array_map()与array_shift()搭配使用 PK array_column()函数
array_map()与arra_shift()搭配使用,还是来看例子吧,比较直观一点 <?php $user = array( 0 => array( 'name' => '张三' ...
- 由Collections.unmodifiableList引发的重构
原文 http://www.cnblogs.com/persist-confident/p/4516741.html 今天阅读源码的时候,无意中看到了Collections.unmodifiable ...
- base64 加密
Base64 参考网站:http://zh.wikipedia.org/wiki/Base64 简介 是网络上使用最广泛的编码系统,能够将任何二进制数据,转换成只有 65 个字符组成的文本文件 a~z ...
- 【8-22】java学习笔记04
java基础类库 Scanner类(java.util.scanner) Scanner对象.hasNextXxx(),hasNext()默认方法为字符串://Returns true if this ...
- localdomain的linux域
both和and的区别? both强调(两者)都,用于肯定语气, and字面意思是"和,而且",是用于表示并列关系的句子,通常位于最后两个人或物之间,将将人物串联起来 如: thi ...