Results 1 to 5 of 5
Thread: Reading a txt file
- 01-26-2010, 11:03 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Reading a txt file
Hello... i'm a beginner with java and so i new your help!
i've just tried to create a java program which should read a txt file and then shows the content... but i don't know the reason why it doesn't work! could you check it? when the program works i don't see an error report, but it doesn't show me anythink!
This is the program
import java.io.;
import java.util.;
class LeggiFile
{
// costruttore
public LeggiFile() {}
public void leggeToken()
{
FileReader f = null;
BufferedReader fIN = null;
String s;
try
{
f = new FileReader("diego.txt");
fIN = new BufferedReader(f);
}
catch (IOException e)
{
System.out.println("Errore nell'apertura del file");
System.exit(1);
}
try
{
s = fIN.readLine();
while (s != null)
{
// operazioni sulla stringa s
s = fIN.readLine();
}
}
catch (IOException e)
{
System.out.println("Errore nella lettura del file.");
System.exit(1);
}
try
{
f.close();
}
catch (IOException e)
{
System.out.println("Errore nella chiusura del file.");
System.exit(1);
}
}
public static void main(String args[])
{
LeggiFile a = new LeggiFile();
a.leggeToken();
}
}Last edited by diegosened; 01-26-2010 at 11:25 PM.
- 01-26-2010, 11:49 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Does this compile? I ask because the import statements look a little weird.
Anyway in your while loop you read the file but you don't do anything with the strings that are read. Try:
Java Code:while (s != null) { // operazioni sulla stringa s System.out.println(s); s = fIN.readLine(); }
- 01-26-2010, 11:51 PM #3
Two things:
1 — add an asterisk to the end of your import statements
2 — write out each line to the console:Java Code:import java.io.*; import java.util.*;
Java Code:try { s = fIN.readLine(); while (s != null) { // operazioni sulla stringa s [b]System.out.println(s);[/b] s = fIN.readLine(); } }
- 01-27-2010, 11:08 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
thanks!!! i made it!!!!
i wanna ask you another think..
my goal now is that each word (contained in the txt file) should be seen line per line.
For example
txt file: i do not know java
i have to see in the video:
i
do
know
java
could i do this?
sorry for my bad grammar!
this is the program
Java Code:import java.io.*; import java.util.*; class FileRead { public static void main(String args[]) { FileRead f = new FileRead(); f.readMyFile(); } public void readMyFile() { FileReader f = null; BufferedReader fIN = null; String s; try { f = new FileReader("diego.txt"); fIN = new BufferedReader(f); s = fIN.readLine(); while(s != null) { System.out.println(s); s = fIN.readLine(); } } catch (IOException e) { System.out.println("Errore nella lettura del file"); } finally { if(fIN != null) { try { fIN.close(); } catch (IOException e) { System.out.println("Errore nella chiusura del file."); } } } } }Last edited by diegosened; 01-27-2010 at 11:59 AM.
- 01-27-2010, 01:05 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Help reading file
By fritz1474 in forum New To JavaReplies: 4Last Post: 09-17-2008, 03:28 AM -
Reading a file
By mew in forum New To JavaReplies: 2Last Post: 12-30-2007, 12:23 PM -
Reading a file for use
By peachyco in forum New To JavaReplies: 2Last Post: 11-27-2007, 03:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks