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.. - import java.io.*;
-
import java.util.*;
-
-
class fileClass {
-
char [] charArr;
-
-
public String getFileMeth() throws IOException
-
{
-
-
int i;
-
FileInputStream fin;
-
String str=null;
-
char ch=0;
-
-
-
fin = new FileInputStream("hi.txt");
-
int j=0;
-
do {
-
-
i = fin.read();
-
//System.out.println(i);
-
if(i != -1){
-
System.out.println(i);
-
charArr=new char[i];
-
//System.out.println(charArr.length);
-
ch=(char)i;
-
-
if(j<charArr.length)
-
{
-
//System.out.println(j);
-
charArr[j]=ch;
-
//System.out.println(charArr[j]);
-
-
j++;
-
-
}
-
}
-
} while(i != -1);
-
-
fin.close();
-
-
//for(int z=0;z<charArr.length;z++)
-
//System.out.println(charArr[z]);
-
-
-
-
-
str=new String(charArr);
-
-
-
return str;
-
-
}
-
-
-
public static void main(String args[])
-
throws IOException
-
{
-
-
fileClass fc=new fileClass();
-
try {
-
String fileRead=fc.getFileMeth();
-
-
} catch(FileNotFoundException e) {
-
System.out.println("File Not Found");}
-
catch(ArrayIndexOutOfBoundsException e) {
-
System.out.println("Usage: ShowFile File");}
-
}
-
}
-
TIA...
20 1491
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.. - import java.io.*;
-
import java.util.*;
-
-
class fileClass {
-
char [] charArr;
-
-
public String getFileMeth() throws IOException
-
{
-
-
int i;
-
FileInputStream fin;
-
String str=null;
-
char ch=0;
-
-
-
fin = new FileInputStream("hi.txt");
-
int j=0;
-
do {
-
-
i = fin.read();
-
//System.out.println(i);
-
if(i != -1){
-
System.out.println(i);
-
charArr=new char[i];
-
//System.out.println(charArr.length);
-
ch=(char)i;
-
-
if(j<charArr.length)
-
{
-
//System.out.println(j);
-
charArr[j]=ch;
-
//System.out.println(charArr[j]);
-
-
j++;
-
-
}
-
}
-
} while(i != -1);
-
-
fin.close();
-
-
//for(int z=0;z<charArr.length;z++)
-
//System.out.println(charArr[z]);
-
-
-
-
-
str=new String(charArr);
-
-
-
return str;
-
-
}
-
-
-
public static void main(String args[])
-
throws IOException
-
{
-
-
fileClass fc=new fileClass();
-
try {
-
String fileRead=fc.getFileMeth();
-
-
} catch(FileNotFoundException e) {
-
System.out.println("File Not Found");}
-
catch(ArrayIndexOutOfBoundsException e) {
-
System.out.println("Usage: ShowFile File");}
-
}
-
}
-
TIA...
Don't use Code Words.
Have a look at this. -
FileInputStream in = new FileInputStream("file");
-
byte b;
-
while((b=in.read)!=-1) System.out.print((char)b);
-
Debasis Jana
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.
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... - import java.io.*;
-
import java.util.*;
-
-
public class file2 {
-
char [] charArr;
-
-
public String getFileMeth() throws IOException
-
{
-
-
byte b;
-
FileInputStream fin;
-
String str=null;
-
char ch=0;
-
fin = new FileInputStream("hi.txt");
-
charArr=new char[fin.available()];
-
int j=0;
-
while((b=(byte)fin.read())!=-1) {
-
ch=(char)b;
-
if(j<charArr.length)
-
{
-
//System.out.println(j);
-
charArr[j]=ch;
-
//System.out.println(charArr[j]);
-
j++;
-
}
-
-
}
-
fin.close();
-
str=new String(charArr);
-
System.out.println(str);
-
return str;
-
}
-
-
public void setFileMeth(String fileRead) throws IOException
-
{
-
System.out.println("fileRead "+fileRead);
-
-
StringTokenizer sz=new StringTokenizer(fileRead);
-
System.out.println("sz "+sz);
-
FileOutputStream fout;
-
String str=null;
-
-
while(sz.hasMoreElements())
-
{
-
str=sz.nextToken();
-
String len=String.valueOf(str.length());
-
-
byte b[]=str.getBytes();
-
fout=new FileOutputStream(len + ".txt");
-
for(int y=0;y<b.length;y++)
-
{
-
fout.write(b[y]);
-
}
-
}
-
-
}
-
public static void main(String args[])
-
throws IOException
-
{
-
-
file2 fc=new file2();
-
try {
-
String fileRead=fc.getFileMeth();
-
fc.setFileMeth(fileRead);
-
-
} catch(FileNotFoundException e) {
-
System.out.println("File Not Found");}
-
catch(ArrayIndexOutOfBoundsException e) {
-
System.out.println("Usage: ShowFile File");}
-
}
-
}
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... - import java.io.*;
-
import java.util.*;
-
-
public class file2 {
-
char [] charArr;
-
-
public String getFileMeth() throws IOException
-
{
-
-
byte b;
-
FileInputStream fin;
-
String str=null;
-
char ch=0;
-
fin = new FileInputStream("hi.txt");
-
charArr=new char[fin.available()];
-
int j=0;
-
while((b=(byte)fin.read())!=-1) {
-
ch=(char)b;
-
if(j<charArr.length)
-
{
-
//System.out.println(j);
-
charArr[j]=ch;
-
//System.out.println(charArr[j]);
-
j++;
-
}
-
-
}
-
fin.close();
-
str=new String(charArr);
-
System.out.println(str);
-
return str;
-
}
-
-
public void setFileMeth(String fileRead) throws IOException
-
{
-
System.out.println("fileRead "+fileRead);
-
-
StringTokenizer sz=new StringTokenizer(fileRead);
-
System.out.println("sz "+sz);
-
FileOutputStream fout;
-
String str=null;
-
-
while(sz.hasMoreElements())
-
{
-
str=sz.nextToken();
-
String len=String.valueOf(str.length());
-
-
byte b[]=str.getBytes();
-
fout=new FileOutputStream(len + ".txt");
-
for(int y=0;y<b.length;y++)
-
{
-
fout.write(b[y]);
-
}
-
}
-
-
}
-
public static void main(String args[])
-
throws IOException
-
{
-
-
file2 fc=new file2();
-
try {
-
String fileRead=fc.getFileMeth();
-
fc.setFileMeth(fileRead);
-
-
} catch(FileNotFoundException e) {
-
System.out.println("File Not Found");}
-
catch(ArrayIndexOutOfBoundsException e) {
-
System.out.println("Usage: ShowFile File");}
-
}
-
}
Suppose you get a string "2" from the file.
Then open a file using .......... -
FileWriter out = new FileWriter("2"+".text");
-
Debasis Jana
Suppose you get a string "2" from the file.
Then open a file using .......... -
FileWriter out = new FileWriter("2"+".text");
-
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...
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 ............. -
FileWriter out = new FileWriter("file_name",true);
-
//it opens the file in append mode
-
Debasis Jana
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?
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...
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
Have you got code worked?
Debasis Jana
No not yet............................................... ........
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
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.
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..........
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...
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.
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
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??
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
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.
Thanks...
I will go through the link and the API and come back in case of queries >>
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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");
...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| |