how to write in a file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 36
Reputation: abar_sow is an unknown quantity at this point 
Solved Threads: 0
abar_sow abar_sow is offline Offline
Light Poster

how to write in a file

 
0
  #1
Sep 14th, 2007
How to write these file datas in the new file...It should be done by wrting bytes into characters

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class fileread
{

public static void main(String[] args)
{
File file = new File("E:\\ir_Proj
estimate_out.txt");

byte[] b = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);

fileInputStream.read(b);
for (int i = 0; i < b.length; i++)
{
System.out.println((char)b);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
catch (IOException e1)
{
System.out.println("Error Reading The File.");
e1.printStackTrace();
}

}
}

thank u
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: how to write in a file

 
0
  #2
Sep 14th, 2007
Well, your question barely makes sense given the code that you posted (which needs code tags too). Maybe this example of writing to a file will help http://www.exampledepot.com/egs/java...iteToFile.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 36
Reputation: abar_sow is an unknown quantity at this point 
Solved Threads: 0
abar_sow abar_sow is offline Offline
Light Poster

Re: how to write in a file

 
0
  #3
Sep 14th, 2007
I already referred wat t link u gave .But still it didnt clear out my doubt.In the previous prog i red a file and i converted tat into byte.If i want to write again tat converted byte into another file how t code shd come??Do u got wat i mean
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to write in a file

 
0
  #4
Sep 14th, 2007
Why are you reading it as bytes.Read and write it as strings you hear me!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 36
Reputation: abar_sow is an unknown quantity at this point 
Solved Threads: 0
abar_sow abar_sow is offline Offline
Light Poster

Re: how to write in a file

 
0
  #5
Sep 14th, 2007
Actually i want to process tat text files, which means am removing sum line in between the paragraphs so i want the correct order to be as my output.As u told if i read in strings and writing in file, means the format is different. All the lines are printing in same line.
Last edited by abar_sow; Sep 14th, 2007 at 4:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 36
Reputation: abar_sow is an unknown quantity at this point 
Solved Threads: 0
abar_sow abar_sow is offline Offline
Light Poster

Re: how to write in a file

 
0
  #6
Sep 15th, 2007
I solved My prob..

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.FilenameFilter;
  6. import java.io.IOException;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10.  
  11. public class Main {
  12.  
  13. public void removeLine(String filename)throws IOException{
  14. BufferedReader br = new BufferedReader(new FileReader("D:\\TextFiles\\" +
  15.  
  16. filename));
  17. FileWriter f2=new FileWriter("D:\\TextFiles\\Output\\" + filename,true);
  18. String s;
  19. //StringBuffer sb;
  20. /*Boolean found;*/
  21.  
  22. while((s=br.readLine())!= null ){
  23. try{
  24. System.out.println(s);
  25. Pattern p = Pattern.compile("(.*)[Ff]ig.?\\s*\\d*\\.\\d*(.*)");
  26. Matcher m = p.matcher (s);
  27. /*found=m.matches ();*/
  28. if(m.find()==true){
  29. continue;
  30. }
  31. s = s + "\r\n";
  32. f2.write(s);
  33. }catch (Exception e){
  34. e.printStackTrace();
  35. }
  36. }
  37. br.close();
  38. f2.close();
  39. }
  40.  
  41. public static void main(String args[])throws IOException {
  42. Main main = new Main();
  43. File file = new File("D:\\TextFiles");
  44.  
  45. FilenameFilter filter = new Onlyext("TXT");
  46. String files[] = file.list(filter);
  47. if(files!=null){
  48. for (int i = 0; i < files.length; i++) {
  49. //System.out.println(files[i]);
  50. main.removeLine(files[i].toString());
  51. }
  52. }
  53. }
  54. }
  55.  
  56. class Onlyext implements FilenameFilter {
  57. String ext;
  58.  
  59. public Onlyext(String ext) {
  60. this.ext = "." + ext;
  61. }
  62.  
  63. public boolean accept(File dir, String name) {
  64. return name.endsWith(ext);
  65. }
  66. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 9095 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC