Skip to content

Instantly share code, notes, and snippets.

@kougazhang
Last active September 9, 2021 03:34
Show Gist options
  • Save kougazhang/aefa11afa0eb9e3d8ed401bf9fabb79e to your computer and use it in GitHub Desktop.
Save kougazhang/aefa11afa0eb9e3d8ed401bf9fabb79e to your computer and use it in GitHub Desktop.
读写文件 #java
@Test
public void toUpyunLog2() throws Exception {
InputStream in = getClass().getResourceAsStream("/aliyun-3");
BufferedReader in2 = new BufferedReader(new InputStreamReader(in));
String lineTxt;
// System.getProperty("user.home") 获取用户主目录
File file = new File(System.getProperty("user.home") + "/aliyun-3-wanted");
// 文件不存在则创建
file.createNewFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
int counter = 0;
while ((lineTxt = in2.readLine()) != null) {
counter++;
String line = ins.toUpyunLog(lineTxt);
// 需要写入分隔符 \n
bw.write(line + "\n");
}
//一定要关闭文件,否则可能有数据留在 buffer 中导致文件内容不完整
bw.close();
fw.close();
System.out.println(counter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment