📄 rwfile.java
字号:
package yus.baseline;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class RWFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long startTime = System.currentTimeMillis();
mergeFile();
separateFile();
long endTime = System.currentTimeMillis();
System.out.println("The spending time is: "
+ String.valueOf(endTime - startTime) + " ms");
}
private static void separateFile() throws IOException {
// TODO Auto-generated method stub
String spath = new File("").getCanonicalPath() + "/text/labeled/";
String dpath = new File("").getCanonicalPath() + "/text/desFile/";
String[] fileArray = { "H1_cla.txt", "H2_cla.txt", "H3_cla.txt",
"H4_cla.txt", "H5_cla.txt", "H6_cla.txt", "H7_cla.txt",
"H8_cla.txt", "H9_cla.txt", "H10_cla.txt" };
int count = 0;
for (int i = 0; i < fileArray.length; i++) {
BufferedReader br = new BufferedReader(new FileReader(spath + "/"
+ fileArray[i]));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null) {
if (!"TheEndOfThisTextOhYeah/nx".equals(line.trim())) {
sb.append(line);
} else {
if (!"".equals(sb.toString().trim())) {
count++;
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(dpath
+ String.valueOf(count) + ".txt"));
osw.write(sb.toString());
osw.flush();
osw.close();
sb = new StringBuffer();
}
}
line = br.readLine();
}
br.close();
}
}
private static void mergeFile() throws IOException {
// TODO Auto-generated method stub
String path = new File("").getCanonicalPath();
String[] pathArray = { path + "/text/C000007", path + "/text/C000008",
path + "/text/C000010", path + "/text/C000013",
path + "/text/C000014", path + "/text/C000016",
path + "/text/C000020", path + "/text/C000022",
path + "/text/C000023", path + "/text/C000024" };
String[] fileArray = { "H1.txt", "H2.txt", "H3.txt", "H4.txt",
"H5.txt", "H6.txt", "H7.txt", "H8.txt", "H9.txt", "H10.txt" };
for (int i = 0; i < pathArray.length; i++) {
File file = new File(pathArray[i]);
StringBuffer sb = new StringBuffer();
if (file.isDirectory()) {
String[] fileList = file.list();
for (String fileName : fileList) {
BufferedReader br2 = new BufferedReader(new FileReader(
pathArray[i] + "/" + fileName));
String line = br2.readLine();
while (line != null) {
sb.append(line);
line = br2.readLine();
}
sb.append("\n\rTheEndOfThisTextOhYeah\n\r");
br2.close();
}
}
String filePath = path + "/text/waitLabel/" + fileArray[i];
File file2 = new File(filePath);
if (file2.exists()) {
file2.delete();
}
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(filePath));
osw.write(sb.toString());
osw.flush();
osw.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -