Bookmark Topic Watch Topic
  • New Topic

how to read one file to write multiple files  RSS feed

 
mahesh waran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
hai,
i've write source code.but not get correct output.

Input:File1.txt
1.hai to all.
2.this program is about file read and write.
3.this is good concept

Output:File2.txt
1.hai to all.
3.this is good concept

File3.txt
2.this program is about file read and write.

Note:Program is read one file and write the alternate line to each file.

Tell me the error in sourcecode.My program was compile and run but not write any content in each file.
Please find the error.I tried more time but not found the error.I gave the source code below.

thanks advance.
Mahesh

SourceCode:

import java.io.*;
import java.lang.*;

public class WriteFile{

public static void main(String []arg) throws IOException{

try{

FileInputStream fin;


fin=new FileInputStream("a.txt");

DataInputStream data1=new DataInputStream(fin);

BufferedReader in=new BufferedReader(new InputStreamReader(data1));

String a;
int i=1,j;

do{

a=in.readLine();
if(a==null)
break;

if((i%2)!=0){
System.out.println(" i value "+i);
j=1;
writef(j,a); // b file
}
else{
System.out.println(" i value "+i);
j=2;
writef(j,a); // c file

}

//out.newLine();
i++;

}while(a!=null);



System.out.println("Writing Finished");


}catch(FileNotFoundException e){

System.out.println(e);

return;

}catch(ArrayIndexOutOfBoundsException e){

System.out.println(e);
return;
}
}

public static void writef(int b,String a){

try{
String filename;
if(b==1){
filename="b.txt";
}
else{
filename="c.txt";
}


FileOutputStream fout=new FileOutputStream(filename);

DataOutputStream data= new DataOutputStream(fout);

BufferedWriter out=new BufferedWriter(new OutputStreamWriter(data));

out.write(a);
out.newLine();

}catch(Exception e){
System.out.println(e);
}
}// end of method

}
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

This looks to be a duplicate post. And since the other post already got a response, please continue there.

http://www.coderanch.com/t/583804/java/java/read-one-file-write-multiple

Henry
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!