处理NSE数据时,会提取和重组一些数据,现在一共有两个方法,一个是 java 自带的方法,一个就是用一个第三方的库 CSVReader。
java 自带的 BufferedReader
import java.io.*;
private static void data_copy() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("your file"));
String line;
String targetFile = "F:\\data analysis";
java.io.File a = new java.io.File(targetFile);
File fileHandle = new File(a,"caochu.txt");
FileOutputStream outStream = new FileOutputStream(fileHandle);
while ((line = br.readLine()) != null)
{
String dataContent = line + "\n";
outStream.write(dataContent.getBytes());
}
outStream.close();
br.close();
}
简单写法如下
import java.io.*;
private static void data_copy() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("file directory"));
String line;
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("target file directory"), StandardCharsets.UTF_8));
while ((line = br.readLine()) != null)
{
writer.write(line + "\n");
}
br.close();
writer.close();
}
使用 CSVReader 库读取
import com.opencsv.CSVReader;
public static void data_extractor() throws IOException, ParseException
{
String[] nextLine;
CSVReader reader = new CSVReader(new FileReader("file directory"));
while ((nextLine = reader.readNext()) != null)
{
FileWriter dataWriter = new FileWriter("target file", false);
PrintWriter dataContent = new PrintWriter(dataWriter);
dataContent.printf("%s", nextLine[1]);
dataContent.printf("%s", "\n");
}
dataContent.close();
}
感觉浪费了好多时间编辑这个东西,不过个人感觉这些东西有一天会有用的。
个人学习资料,如有雷同,那必然是巧合