Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
    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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

how to read one file to write multiple files

 
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

}
 
author
Posts: 23874
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.

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

Henry
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic