Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to read one file to write multiple files

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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

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

Henry
    Bookmark Topic Watch Topic
  • New Topic