本例以Windows, JDK1.7 为基础,这里使用FileOutputStream这个类来,对文件写入操作。
FileOutputStream 写入文件的操作,主要有以下方式:

1
2
3
void	write(byte[] b);
void write(byte[] b, int off, int len);
void write(int b)

写入文件:
FileWriteFileOutputStream.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.devnp.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileWriteFileOutputStream {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
writer();
}

public static void writer() throws IOException{
File file = new File("D:\\Person-Test\\1.txt");

String str = "Hi, Hello, I'm devnp.com" ;

OutputStream outputStream = new FileOutputStream(file);

outputStream.write(65); //this is not 65, is 'A'
outputStream.write(str.getBytes());
outputStream.write(str.getBytes(), 0, 2);

outputStream.close();
}

}