java将一个txt以空行分割成多个怎么实现?
例如abc.txt如下:aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbccccccccccccccccccccccccccc将其分割成三个txt...
例如 abc.txt如下:
aaaaaa
aaaaaaa
aaaaaa
bbbbb
bbbbb
bbbbbbb
cccccccccccc
ccccc
cccccccccc
将其分割成三个txt分别为:
aaaaaa bbbbb cccccccccccc
aaaaaaa bbbbb ccccc
aaaaaa bbbbbbb cccccccccc 展开
aaaaaa
aaaaaaa
aaaaaa
bbbbb
bbbbb
bbbbbbb
cccccccccccc
ccccc
cccccccccc
将其分割成三个txt分别为:
aaaaaa bbbbb cccccccccccc
aaaaaaa bbbbb ccccc
aaaaaa bbbbbbb cccccccccc 展开
1个回答
展开全部
public static void main(String[] args) {
BufferedReader r = null;
BufferedWriter w = null;
try {
r = new BufferedReader(new FileReader(new File("e://abc.txt")));
w = new BufferedWriter(new FileWriter(new File("e://abc1.txt")));
int i=2;
String buff;
while ((buff=r.readLine())!=null) {
w.append(buff).append("\n");
if("".equals(buff)){
w.flush();
w = new BufferedWriter(new FileWriter(new File("e://abc"+i+".txt")));
i++;
}
}
w.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(r!=null){
r.close();
}
if(w!=null){
r.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BufferedReader r = null;
BufferedWriter w = null;
try {
r = new BufferedReader(new FileReader(new File("e://abc.txt")));
w = new BufferedWriter(new FileWriter(new File("e://abc1.txt")));
int i=2;
String buff;
while ((buff=r.readLine())!=null) {
w.append(buff).append("\n");
if("".equals(buff)){
w.flush();
w = new BufferedWriter(new FileWriter(new File("e://abc"+i+".txt")));
i++;
}
}
w.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(r!=null){
r.close();
}
if(w!=null){
r.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询