package exceltest;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 将单个字符写入到文件中,如果文件不存在,则自动创建文件
* */
public class outputTest {
public static void main(String[] args) {
String filepath = "C:\\Users\\ybb\\Desktop\\test1.txt";
FileOutputStream fileOutputStream = null;
int readlen = 0;
try {
// fileOutputStream = new FileOutputStream(filepath);
fileOutputStream = new FileOutputStream(filepath,true);
// fileOutputStream.write('a');//单个字节
String str = "小王去超市买东西";
fileOutputStream.write(str.getBytes());//多个字节
//
// String str1 = "小李去火锅店买羊肉";
// fileOutputStream.write(str1.getBytes(),0,18);//读取长度从0开始,总共读取18个字符
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果:
a小王去超市买东西小李去火锅店小王去超市买东西
每执行一次,在后面就会多出小王去超市买东西这句话