Java I/O 操作示例

给出几个Java I/O 操作的示例代码。

创建文件或目录

import java.io.File;
import java.io.IOException;
public class TestFileIO {
public static void main(String[] args) {
File dir = new File("dir1");
dir.mkdir(); //创建目录
File file = new File(dir,"file1"); //目录加文件名
File file2 = new File("dir1/file2"); //完整路径
try {
file.createNewFile(); //创建文件,若存在同名文件,不会覆盖
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

删除文件

import java.io.File;
public class TestFileIO {
public static void main(String[] args) {
File file = new File("dir1/file2");
if (file.delete()){
System.out.println(file.getName() + " is deleted!");
}else {
System.out.println("File is not deleted!");
}
}
}

向文件逐行写入内容(覆盖写)

  1. FileOutputStream

    import java.io.*;
    public class TestFileIO {
    public static void main(String[] args) throws IOException {
    File fout = new File("dir1/file1");
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    for (int i = 0; i < 10; i++){
    bw.write("something");
    bw.newLine();
    }
    bw.close();
    }
    }
  2. FileWriter

    import java.io.*;
    public class TestFileIO {
    public static void main(String[] args) throws IOException {
    File fout = new File("dir1/file1");
    FileWriter fw = new FileWriter(fout);
    for (int i = 0; i < 10; i++){
    fw.write("something" + System.getProperty("line.separator"));
    }
    fw.close();
    }
    }
  3. PrintWriter

    import java.io.*;
    public class TestFileIO {
    public static void main(String[] args) throws IOException {
    File fout = new File("dir1/file1");
    PrintWriter pw = new PrintWriter(new FileWriter(fout));
    for (int i = 0; i < 10; i++){
    pw.println("something");
    }
    pw.close();
    }
    }
  4. OutputStreamWriter

    import java.io.*;
    public class TestFileIO {
    public static void main(String[] args) throws IOException {
    File fout = new File("dir1/file1");
    FileOutputStream fos = new FileOutputStream(fout);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    for (int i = 0; i < 10; i++) {
    osw.write("something" + System.getProperty("line.separator"));
    }
    osw.close();
    }
    }

注:
往文本文件里写内容用FileWriter即可,比较方便。但是如果要自己定义字符编号和byte-buffer大小的话就要用FileOutputStream。

PrintWriter跟FileWriter的主要区别是PrintWriter可以格式化输出。该类实现了PrintStream的所有print方法。

追加写

import java.io.*;
public class TestFileIO {
public static void main(String[] args) throws IOException {
File fout = new File("dir1/file1");
FileOutputStream fos = new FileOutputStream(fout,true); //跟覆盖写唯一的区别是这里加了个true参数。
OutputStreamWriter osw = new OutputStreamWriter(fos);
for (int i = 0; i < 10; i++) {
osw.write("something" + System.getProperty("line.separator"));
}
osw.close();
}
}

拷贝文件

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestFileIO {
public static void main(String[] args) throws IOException {
Path sour = Paths.get("dir1/file1");
Path des = Paths.get("dir1/file2");
Files.copy(sour,des); //Files.copy(a,b)。
}
}

合并多个文件

读取多个文件的内容,写入一个文件。

import java.io.*;
/**
* Created by lbd on 2017/1/13.
*/
public class MergeFiles {
public static void main(String[] args) throws IOException {
String sourceFile1Path = "dir1/file1";
String sourceFile2Path = "dir1/file2";
String mergedFilePath = "dir1/mergedFile.txt";
File[] files = new File[2];
files[0] = new File(sourceFile1Path);
files[1] = new File(sourceFile2Path);
File mergedFile = new File(mergedFilePath);
mergeFiles(files,mergedFile);
}
public static void mergeFiles(File[] files,File mergedFile) throws IOException {
FileWriter fw = new FileWriter(mergedFile,true);
BufferedWriter bw = new BufferedWriter(fw);
for (File f : files){
System.out.println("merging: " + f.getName());
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String aLine;
while ((aLine = br.readLine()) != null){
bw.write(aLine);
bw.newLine();
}
br.close();
}
bw.close();
}
}

移动文件

调用的是File.renameTo()方法。

import java.io.*;
public class MoveFile {
public static void main(String[] args) throws IOException {
File f1 = new File("dir1/file1");
File f2 = new File("dir1/dir2/file3"); //dir2目录必须存在,否则无法移动成功
f1.renameTo(f2);
}
}

对文件内容排序

file1内容如下:

dog
cat
--windows
--kankan
pps
game
--annot be guaranteed
as it is, generally speaking,
--impossible to make any hard gu
arantees in the p
--resence of unsynchr

对行进行排序,以上面的文本为例,排序后arantees in the p应该在第一行

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by lbd on 2017/1/13.
*/
public class TestJavaIO {
public static void main(String[] args) throws IOException {
File fin = new File("file1");
File fout = new File("file2");
String s;
FileWriter fw = new FileWriter(fout);
FileReader fr = new FileReader(fin);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
ArrayList<String> al = new ArrayList<>();
while ((s = br.readLine()) != null ){
if (!s.trim().startsWith("-") && s.trim().length() > 0){
al.add(s);
}
}
Collections.sort(al);
for (String line : al){
bw.write(line);
bw.newLine();
bw.write("------------------------------");
bw.newLine();
}
br.close();
bw.close();
}
}

file2内容如下:

arantees in the p
------------------------------
as it is, generally speaking,
------------------------------
cat
------------------------------
dog
------------------------------
game
------------------------------
pps
------------------------------

欢迎关注公众号: FullStackPlan 获取更多干货

Copyright © 2016 - 2017 LBD All Rights Reserved.

访客数 : | 访问量 :