473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

I/O Streams Problem

snowfall
Am trying to read from a file and print it in the console. But while running the code, just getting the last letter present in my text file and lots of empty spaces. Pls help me to find the error..

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class fileClass {
  5.     char [] charArr;
  6.  
  7. public String getFileMeth() throws IOException
  8. {
  9.  
  10.     int i;
  11.     FileInputStream fin;
  12.     String str=null;
  13.     char ch=0;
  14.  
  15.  
  16.     fin = new FileInputStream("hi.txt");
  17.     int j=0;
  18.     do {
  19.  
  20.     i = fin.read();
  21.     //System.out.println(i);
  22.     if(i != -1){
  23.     System.out.println(i);
  24.     charArr=new char[i];
  25.     //System.out.println(charArr.length);
  26.     ch=(char)i;
  27.  
  28.     if(j<charArr.length)
  29.     {
  30.         //System.out.println(j);
  31.         charArr[j]=ch;
  32.         //System.out.println(charArr[j]);
  33.  
  34.         j++;
  35.  
  36.     }
  37.     }
  38. } while(i != -1);
  39.  
  40.     fin.close();
  41.  
  42. //for(int z=0;z<charArr.length;z++)
  43. //System.out.println(charArr[z]);
  44.  
  45.  
  46.  
  47.  
  48. str=new String(charArr);
  49.  
  50.  
  51. return str;
  52.  
  53. }
  54.  
  55.  
  56. public static void main(String args[])
  57. throws IOException
  58. {
  59.  
  60. fileClass fc=new fileClass();
  61. try {
  62. String fileRead=fc.getFileMeth();
  63.  
  64. } catch(FileNotFoundException e) {
  65.     System.out.println("File Not Found");}
  66.     catch(ArrayIndexOutOfBoundsException e) {
  67.     System.out.println("Usage: ShowFile File");}
  68. }
  69. }
  70.  
TIA...
Oct 9 '07 #1
20 1491
dmjpro
2,476 2GB
Am trying to read from a file and print it in the console. But while running the code, just getting the last letter present in my text file and lots of empty spaces. Pls help me to find the error..

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class fileClass {
  5.     char [] charArr;
  6.  
  7. public String getFileMeth() throws IOException
  8. {
  9.  
  10.     int i;
  11.     FileInputStream fin;
  12.     String str=null;
  13.     char ch=0;
  14.  
  15.  
  16.     fin = new FileInputStream("hi.txt");
  17.     int j=0;
  18.     do {
  19.  
  20.     i = fin.read();
  21.     //System.out.println(i);
  22.     if(i != -1){
  23.     System.out.println(i);
  24.     charArr=new char[i];
  25.     //System.out.println(charArr.length);
  26.     ch=(char)i;
  27.  
  28.     if(j<charArr.length)
  29.     {
  30.         //System.out.println(j);
  31.         charArr[j]=ch;
  32.         //System.out.println(charArr[j]);
  33.  
  34.         j++;
  35.  
  36.     }
  37.     }
  38. } while(i != -1);
  39.  
  40.     fin.close();
  41.  
  42. //for(int z=0;z<charArr.length;z++)
  43. //System.out.println(charArr[z]);
  44.  
  45.  
  46.  
  47.  
  48. str=new String(charArr);
  49.  
  50.  
  51. return str;
  52.  
  53. }
  54.  
  55.  
  56. public static void main(String args[])
  57. throws IOException
  58. {
  59.  
  60. fileClass fc=new fileClass();
  61. try {
  62. String fileRead=fc.getFileMeth();
  63.  
  64. } catch(FileNotFoundException e) {
  65.     System.out.println("File Not Found");}
  66.     catch(ArrayIndexOutOfBoundsException e) {
  67.     System.out.println("Usage: ShowFile File");}
  68. }
  69. }
  70.  
TIA...
Don't use Code Words.
Have a look at this.

Expand|Select|Wrap|Line Numbers
  1. FileInputStream in = new FileInputStream("file");
  2. byte b;
  3. while((b=in.read)!=-1) System.out.print((char)b);
  4.  
Debasis Jana
Oct 9 '07 #2
r035198x
13,262 8TB
Do not use XXXInputStreams for reading text files. That is the the very wrong way of doing it.

Use FileReader or Scanner as explained here.
Oct 9 '07 #3
Hi Thnks for the reply...

ro, i will try using File and see...

but now my problem is, am able to read the file and according to the number of letters in the words in my text file, am creating files. Eg 2 letter word means 2.txt etc..

The problem is instead of appending to the output file, its getting overwritten..

Pls help me...

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class file2 {
  5.     char [] charArr;
  6.  
  7. public String getFileMeth() throws IOException
  8. {
  9.  
  10.     byte b;
  11.     FileInputStream fin;
  12.     String str=null;
  13.     char ch=0;
  14.     fin = new FileInputStream("hi.txt");
  15.     charArr=new char[fin.available()];
  16.     int j=0;
  17.     while((b=(byte)fin.read())!=-1) {
  18.     ch=(char)b;
  19.     if(j<charArr.length)
  20.     {
  21.         //System.out.println(j);
  22.         charArr[j]=ch;
  23.         //System.out.println(charArr[j]);
  24.         j++;
  25.     }
  26.  
  27. }
  28. fin.close();
  29. str=new String(charArr);
  30. System.out.println(str);
  31. return str;
  32. }
  33.  
  34. public void setFileMeth(String fileRead) throws IOException
  35. {
  36.     System.out.println("fileRead "+fileRead);
  37.  
  38. StringTokenizer sz=new StringTokenizer(fileRead);
  39. System.out.println("sz "+sz);
  40. FileOutputStream fout;
  41. String str=null;
  42.  
  43. while(sz.hasMoreElements())
  44. {
  45. str=sz.nextToken();
  46. String len=String.valueOf(str.length());
  47.  
  48. byte b[]=str.getBytes();
  49. fout=new FileOutputStream(len + ".txt");
  50. for(int y=0;y<b.length;y++)
  51. {
  52.     fout.write(b[y]);
  53. }
  54. }
  55.  
  56. }
  57. public static void main(String args[])
  58. throws IOException
  59. {
  60.  
  61. file2 fc=new file2();
  62. try {
  63. String fileRead=fc.getFileMeth();
  64. fc.setFileMeth(fileRead);
  65.  
  66. } catch(FileNotFoundException e) {
  67.     System.out.println("File Not Found");}
  68.     catch(ArrayIndexOutOfBoundsException e) {
  69.     System.out.println("Usage: ShowFile File");}
  70. }
  71. }
Oct 11 '07 #4
dmjpro
2,476 2GB
Hi Thnks for the reply...

ro, i will try using File and see...

but now my problem is, am able to read the file and according to the number of letters in the words in my text file, am creating files. Eg 2 letter word means 2.txt etc..

The problem is instead of appending to the output file, its getting overwritten..

Pls help me...

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class file2 {
  5.     char [] charArr;
  6.  
  7. public String getFileMeth() throws IOException
  8. {
  9.  
  10.     byte b;
  11.     FileInputStream fin;
  12.     String str=null;
  13.     char ch=0;
  14.     fin = new FileInputStream("hi.txt");
  15.     charArr=new char[fin.available()];
  16.     int j=0;
  17.     while((b=(byte)fin.read())!=-1) {
  18.     ch=(char)b;
  19.     if(j<charArr.length)
  20.     {
  21.         //System.out.println(j);
  22.         charArr[j]=ch;
  23.         //System.out.println(charArr[j]);
  24.         j++;
  25.     }
  26.  
  27. }
  28. fin.close();
  29. str=new String(charArr);
  30. System.out.println(str);
  31. return str;
  32. }
  33.  
  34. public void setFileMeth(String fileRead) throws IOException
  35. {
  36.     System.out.println("fileRead "+fileRead);
  37.  
  38. StringTokenizer sz=new StringTokenizer(fileRead);
  39. System.out.println("sz "+sz);
  40. FileOutputStream fout;
  41. String str=null;
  42.  
  43. while(sz.hasMoreElements())
  44. {
  45. str=sz.nextToken();
  46. String len=String.valueOf(str.length());
  47.  
  48. byte b[]=str.getBytes();
  49. fout=new FileOutputStream(len + ".txt");
  50. for(int y=0;y<b.length;y++)
  51. {
  52.     fout.write(b[y]);
  53. }
  54. }
  55.  
  56. }
  57. public static void main(String args[])
  58. throws IOException
  59. {
  60.  
  61. file2 fc=new file2();
  62. try {
  63. String fileRead=fc.getFileMeth();
  64. fc.setFileMeth(fileRead);
  65.  
  66. } catch(FileNotFoundException e) {
  67.     System.out.println("File Not Found");}
  68.     catch(ArrayIndexOutOfBoundsException e) {
  69.     System.out.println("Usage: ShowFile File");}
  70. }
  71. }

Suppose you get a string "2" from the file.
Then open a file using ..........
Expand|Select|Wrap|Line Numbers
  1. FileWriter out = new FileWriter("2"+".text");
  2.  
Debasis Jana
Oct 11 '07 #5
Suppose you get a string "2" from the file.
Then open a file using ..........
Expand|Select|Wrap|Line Numbers
  1. FileWriter out = new FileWriter("2"+".text");
  2.  
Debasis Jana
Am able to create the files.. Thats not the problem...

If my input file is having "This is Java", then two files 2.txt & 4.txt are gettting created.
But in 4.txt only Java is there.. "This" is not there...
Meaning while writing into the file, its overwriting instead of appening..

Pls help me...
Oct 11 '07 #6
dmjpro
2,476 2GB
Am able to create the files.. Thats not the problem...

If my input file is having "This is Java", then two files 2.txt & 4.txt are gettting created.
But in 4.txt only Java is there.. "This" is not there...
Meaning while writing into the file, its overwriting instead of appening..

Pls help me...
So open the file using .............

Expand|Select|Wrap|Line Numbers
  1. FileWriter out = new FileWriter("file_name",true);
  2. //it opens the file in append mode
  3.  
Debasis Jana
Oct 11 '07 #7
r035198x
13,262 8TB
Hi Thnks for the reply...

ro, i will try using File and see...

but now my problem is, am able to read the file and according to the number of letters in the words in my text file, am creating files. Eg 2 letter word means 2.txt etc..

The problem is instead of appending to the output file, its getting overwritten..

Pls help me...
Did you read the link I posted above?
Oct 11 '07 #8
Did you read the link I posted above?
Ya read it ro..
But as i have started this way, i want to get the solution for this program itself...
Once i get the output, i will modify the code with FileReader & Writer :-)

btw the link was too good.. It was an eye opener for I/O..

Thanks ro...
Oct 12 '07 #9
dmjpro
2,476 2GB
Ya read it ro..
But as i have started this way, i want to get the solution for this program itself...
Once i get the output, i will modify the code with FileReader & Writer :-)

btw the link was too good.. It was an eye opener for I/O..

Thanks ro...

Have you got code worked?

Debasis Jana
Oct 12 '07 #10
Have you got code worked?

Debasis Jana
No not yet............................................... ........
Oct 12 '07 #11
dmjpro
2,476 2GB
No not yet............................................... ........
Ok let's restart ............

"If my input file is having "This is Java", then two files 2.txt & 4.txt are gettting created.
But in 4.txt only Java is there.. "This" is not there...
Meaning while writing into the file, its overwriting instead of appening.."

Tell me elaborately ... what does it mean?

Debasis Jana
Oct 12 '07 #12
r035198x
13,262 8TB
Ya read it ro..
But as i have started this way, i want to get the solution for this program itself...
Once i get the output, i will modify the code with FileReader & Writer :-)

btw the link was too good.. It was an eye opener for I/O..

Thanks ro...
The link also explains how to open a file in append mode.
Oct 12 '07 #13
The link also explains how to open a file in append mode.
Thanks ro..............
Its working fine now...

Sorry, i didnt check the link properly..........
Oct 12 '07 #14
Ok let's restart ............

"If my input file is having "This is Java", then two files 2.txt & 4.txt are gettting created.
But in 4.txt only Java is there.. "This" is not there...
Meaning while writing into the file, its overwriting instead of appening.."

Tell me elaborately ... what does it mean?

Debasis Jana
I didnt know how to append to a file...
With ref to ro's link i used new FileOutputStream(filename,true);

Now, each time when i write to the file its appending...
Oct 12 '07 #15
r035198x
13,262 8TB
I didnt know how to append to a file...
With ref to ro's link i used new FileOutputStream(filename,true);

Now, each time when i write to the file its appending...
But I said not to use a stream to write to a text file.
Use a FileWriter wrapped in a BufferedWriter as explained in that link.
Oct 12 '07 #16
dmjpro
2,476 2GB
I didnt know how to append to a file...
With ref to ro's link i used new FileOutputStream(filename,true);

Now, each time when i write to the file its appending...
You didn't see my code.
There I passed the second parameter as "true".

Debasis Jana
Oct 12 '07 #17
But I said not to use a stream to write to a text file.
Use a FileWriter wrapped in a BufferedWriter as explained in that link.
Ya i have changed it to FileWriter..

Can you tell me why we should use Reader & Writer instead of Streams??
Oct 12 '07 #18
dmjpro
2,476 2GB
Ya i have changed it to FileWriter..

Can you tell me why we should use Reader & Writer instead of Streams??
Streams for BYTE stream.
And Reader or Writer for text Stream.
Read the document for details information.

Debasis Jana
Oct 12 '07 #19
r035198x
13,262 8TB
Ya i have changed it to FileWriter..

Can you tell me why we should use Reader & Writer instead of Streams??
Here is one of the many articles written on the subject.
Oct 12 '07 #20
Thanks...

I will go through the link and the API and come back in case of queries >>
Oct 12 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ronald Legere | last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is that there seems to be no way to copy an iterator. How does one work around this? Is there a trick that I am missing? ...
3
by: sb | last post by:
I think streams are nice, but what do you do when you have to write to or, even worse, read from a FILE*, for example a UNIX stream? C++ streams can not be created from FILE*'s or have them...
5
by: ferran | last post by:
Hi, I'm trying to convert a string to a long double using streams but for some reasing seems to give the wrong values, the idea is to do a precise textual conversion without roundings or...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
11
by: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed....
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
9
by: Carsten H. Pedersen | last post by:
Hello. Having an issue with double streams... for a lack of a better name. :) I have the following two streams: - FooInputStream, fis, extending InputStream - FooOutputStream, fos, extending...
14
by: Gaijinco | last post by:
I used C for a long time where I could open a file for input but if the file didn't existed, then I redirected the input to the standard input, something like: FILE *id=fopen("data.in","r"); ...
3
by: Kirit Sælensminde | last post by:
>From thread http://groups.google.com/group/comp.lang.c++/browse_thread/thread/79d767efa42df516 "P.J. Plauger" <p...@dinkumware.comwrites: I'll take this at face value and I'll have to suppose...
1
by: woessner | last post by:
This is not a really well-formed question so please bear with me. I have a very large binary file (tens of GB). I would like to write a quick program to take a contiguous piece of the file and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.