1.创建文件
createNewFile() 只能创建文件,当testMkdir文件夹不存在时:Exception in thread "main" java.io.IOException: 系统找不到指定的路径。
mkdir() 只能创建文件夹,且为单层文件夹,如"F:/testMkdir" , 若创建"F:/testMkdir/test001.txt"则不会创建,也不会异常
mkdirs() 也只能创建文件夹,但可以创建多层文件夹,如"F:/testMkdir/test001.txt",其中"test001.txt"是文件夹,不是文件
File file = new File("F:/testMkdir/test001.txt");
if (!file.exists()){
file.createNewFile();
}
2.字节流
public static void main(String[] args) throws IOException {
// 新建文件
File file = new File("F:/test.txt");
writeOutput(file);
System.out.println(readInput(file));
}
public static void writeOutput(File file) throws IOException {
// 创建输出流 把str输出到文件中
//FileOutputStream os = new FileOutputStream(file,true);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
String str = "写入内容";
bos.write(str.getBytes());
// 关闭流
bos.close();
}
public static String readInput(File file) throws IOException {
// 创建输入流 将文件中的内容读入
//FileInputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
// 一次读取多少个字节
byte[] bytes = new byte[1024];
// 用来接收读取到的字节
StringBuffer sb = new StringBuffer();
// 读取到的字节长度 为-1时表示没有数据了
int length = 0;
while ((length = bis.read(bytes)) != -1){
// 将读取的内容转换成字符串
sb.append(new String(bytes,0,length));
}
// 关闭流
bis.close();
return sb.toString();
}
3.字符流
public static void main(String[] args) throws IOException {
File file = new File("F:/text01.txt");
write(file);
System.out.println(read(file));
}
private static String read(File file) throws IOException {
FileReader fr = new FileReader(file);
char[] chars = new char[1024];
StringBuffer sb = new StringBuffer();
int length;
while ((length = fr.read(chars)) != -1){
sb.append(chars,0,length);
}
fr.close();
return sb.toString();
}
private static void write(File file) throws IOException {
FileWriter fw = new FileWriter(file, true);
fw.write("第san次测试");
fw.close();
}
4.字符缓冲流
public static void main(String[] args) throws IOException {
File file = new File("F:/text01.txt");
write(file);
System.out.println(bufferedRead(file));
}
private static String bufferedRead(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String line; // 按行读取数据
while ((line = br.readLine()) != null){
sb.append(line);
}
br.close();
return sb.toString();
}
private static void write(File file) throws IOException {
//FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write("字符缓冲流");
bw.close();
}
5.练习
先在write文件中输出"云深不知处",然后从write文件中读取,再输出到read文件中
public static void main(String[] args) throws IOException {
File writeFile = new File("F:/test/writeTest/write.txt");
File readFile = new File("F:/test/readTest/read.txt");
write(writeFile);
readAndWrite(writeFile,readFile);
}
public static void write(File file) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write("云深不知处");
bw.close();
}
public static void readAndWrite(File writeFile, File readFile) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(writeFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(readFile));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null){
// 读一行存一行
bw.write(line);
}
br.close();
bw.close();
}