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
}