Results 1 to 5 of 5
  1. #1
    diegosened is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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();
    }

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Two things:
    1 add an asterisk to the end of your import statements
    Java Code:
    import java.io.*;
    import java.util.*;
    2 write out each line to the console:
    Java Code:
            try
            {
                s = fIN.readLine();
                while (s != null)
                {
                    // operazioni sulla stringa s
                    [b]System.out.println(s);[/b]
                    s = fIN.readLine();
                }
            }

  4. #4
    diegosened is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

Similar Threads

  1. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  2. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  3. Help reading file
    By fritz1474 in forum New To Java
    Replies: 4
    Last Post: 09-17-2008, 03:28 AM
  4. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 12:23 PM
  5. Reading a file for use
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 03:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •