|
3Java金币
timestep
1
adaskdajkcmnaslzcmn
aeqwe
timestep
2
asdakdjalfjasl
qweq
eqweqr
timestep
3
djakslf;lsnfsdancask
qeq
timestep
4
adaklfjls;aj;lajfakl
类似这样的文件
想把timestep1,2放一个文件
timestep,3,4放一个文件
小白一只,求大神指点····
我试过的编程可是不行
代码如下
package test1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class littletest {
public static void readFileByLines(String fileName){
int count=0;
int i=0;
// int lineDivision=0;
boolean checkLine = false;
boolean checkDivision = false;
File sourceFile=new File(fileName);
BufferedReader reader=null;
BufferedWriter bw =null;
try{
while(true){
if(count ==4)
{
break;
}
else if(checkLine == true){
i++;
checkLine = false;
}
String targetFilePath="D:/test/target/output"+i+".txt";
//System.out.println(targetFilePath);
File targetFile=new File(targetFilePath);
if (targetFile.exists()) {
// System.out.println("存在文件夹或者文件test");
} else {
try {
targetFile.createNewFile(); // 文件的创建,注意与文件夹创建的区别
} catch (IOException e) {
e.printStackTrace();
}
}
if(i == 0){
reader=new BufferedReader(new FileReader(sourceFile));
}
FileWriter fw =new FileWriter(targetFile);
bw = new BufferedWriter(fw);
String tempString=null;
while((tempString=reader.readLine())!=null){
fw.write(tempString+"\n");
System.out.println(tempString+"\n");
if(tempString.equals("timestep")==true)
{
checkDivision = true;
count++;
}
// lineDivision++;
//
// if(lineDivision == 3)
// lineDivision = 0;
if( count%2 == 0 )
{
System.out.println(count);
break;
}
else if (checkLine=false)
{
continue;
}
}
bw.close();
}
reader.close();
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOException e1){
e1.printStackTrace();
}
}
}
}
public static void main(String[]args)throws IOException{
readFileByLines("D:/test/source/fengetest.txt");
}
}
|
|