944,168 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 14435
  • Java RSS
Sep 14th, 2007
0

how to write in a file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
abar_sow is offline Offline
36 posts
since Jul 2007
Sep 14th, 2007
0

Re: how to write in a file

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 14th, 2007
0

Re: how to write in a file

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
Reputation Points: 10
Solved Threads: 0
Light Poster
abar_sow is offline Offline
36 posts
since Jul 2007
Sep 14th, 2007
0

Re: how to write in a file

Why are you reading it as bytes.Read and write it as strings you hear me!
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 14th, 2007
0

Re: how to write in a file

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
abar_sow is offline Offline
36 posts
since Jul 2007
Sep 15th, 2007
0

Re: how to write in a file

I solved My prob..

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
abar_sow is offline Offline
36 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: please help me to build a java project.
Next Thread in Java Forum Timeline: Do desktop applications rely on database software?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC