inputstream 生成多个文件?
inputStream 内容如下:
文件,内容用"|"隔开的
1.txt|{内容}|2.txt|{内容}|
怎么生成多个文本文件?
------解决方案--------------------
package net.csdn.question;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.Writer;
public class FileSpliter {
public static void main(String[] args) throws IOException{
InputStream is = new FileInputStream(new File("C:/123.txt"));
Reader in = new InputStreamReader(is);
BufferedReader br = new BufferedReader(in);
StringBuffer strBuffer = new StringBuffer();
while(true){
String temp = br.readLine();
if(temp != null){
strBuffer.append(temp);
}else{
br.close();
break;
}
}
String[] content = strBuffer.toString().split("\\
------解决方案--------------------
");
for(int i = 0;i<content.length;i++){
File subFile = new File("c:/sub123_"+i+".txt");
if(!subFile.exists())
subFile.createNewFile();
Writer writer = new FileWriter(subFile);
writer.write(content[i]);
writer.flush();
writer.close();
}
}
}
你自己在硬盘上建个测试文件,比如我代码里指定的是C:\123.txt