我需要编写一个java代码,用于将大文件拆分为多个较小的文件。
我的文件看起来像
Begin for all values of X
1 abc 5:40
2 pqr 6:40
3 xyz 7:40
End
Begin for all values of Y
1 ccc 9:40
2 ddd 8:40
3 lll 6:40
End
输出将是:
X.txt(Filename)
1 abc 5:40
2 pqr 6:40
3 xyz 7:40
Y.txt(Filename)
1 ccc 9:40
2 ddd 8:40
3 lll 6:40
答案 0 :(得分:0)
你的问题不明确,你可以通过一些表达式将一个字符串拆分成字符串数组......
<p>Input something in the input box:</p>
<div id="ren">
<p ng-if="showName">Name: <input type="text" ng-model="name"></p>
</div>
<p ng-bind="name"></p>
</div>
<button type="button" ng-click="showName!=showName">click</button>
答案 1 :(得分:0)
以下代码可以满足您的需求。要检查文件边界,我测试字符串是否以Begin for all values of
开头,如果您有任何其他条件,则可以将其替换为该字符串。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Test {
public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("input.txt"));
String line = null;
BufferedWriter writer = null;
while( (line = reader.readLine()) != null) {
if(line.startsWith("Begin for all values of")) {
String fileName = line.substring(line.length() -1);
closeWriter(writer);
writer = new BufferedWriter(new FileWriter(fileName + ".txt"));
}
writer.write(line + "\n");
}
closeWriter(writer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void closeWriter(Writer output) {
try {
if(output != null) {
output.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}