/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ludaze
*/
import java.io.*;
import java.util.ArrayList;
public class ReadAndWrite
{
// 创建list存取读出来的数据
ArrayList<String> S_Info = new ArrayList<String>();
/**
* @param args the command line arguments
*/
// ----------------------------------------------------------------------写入文件------------------------------------------------------
// 参考连接 https://blog.csdn.net/szlg510027010/article/details/88974559?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162363563116780274197725%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162363563116780274197725&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-88974559.pc_search_result_cache&utm_term=FileWriter%E5%AF%BC%E5%85%A5&spm=1018.2226.3001.4187
public void WriteStringToFile2(String filePath)
{
try
{
FileWriter fw = new FileWriter(filePath);
fw.write("hijk\n");
fw.write("name");
fw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
// ----------------------------------------------------------------------读取文件--------------------------------------------------------
// 参考链接:https://blog.csdn.net/weixin_30827565/article/details/99909368?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162363699916780262564936%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162363699916780262564936&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-99909368.pc_search_result_cache&utm_term=java+filereader%E8%AF%BB%E5%8F%96%E4%B8%80%E8%A1%8C&spm=1018.2226.3001.4187
// 以行为单位进行读取
public void Read(String filePath)
{
File file = new File(filePath);
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null)
{
S_Info.add(tempString);// 通过循环将每一行存储到S_Info中
}
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
ReadAndWrite rw = new ReadAndWrite();
rw.WriteStringToFile2("dou/write.txt");
rw.Read("dou/write.txt");
System.out.println(rw.S_Info);
}
}
java文件读写
最新推荐文章于 2022-07-20 12:19:24 发布