Can someone point me to some basic Java tutorials?

I’m having a hard time in the official Java tutorials, so can someone post a tutorial for:

  • Reading and writing to text files

Thanks.
Or, perhaps, can you show me how to do it here?

Edit: Never mind, I have a new question. How do you print only certain lines that you want? For example, if I want to print line 4 to line 7 in dialogue.txt, how can I do this?

Here is a simple program to do that. Try checking the javaDoc for any of the classes you don’t know.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;


public class TextTest {
    private final Vector<String> text = new Vector<String>();

    public TextTest(String fileName) throws Exception {
        BufferedReader reader = new BufferedReader( new FileReader( new File(fileName) ) );
        String line;
        while( (line = reader.readLine()) != null ){
            text.add(line);
        }
        reader.close();
    }

    public String getLine(int index){
        return text.get(index);
    }


    public static void main(String[] args) {
        try {
            TextTest test = new TextTest("c:/dialog.txt");
            for(int i = 4; i <= 7; i++){
                System.out.println( test.getLine(i) );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

full list of tutorials: http://home.cogeco.ca/~ve3ll/jatutor0.htm
specific tutorialhttp://home.cogeco.ca/~ve3ll/jatutor9.htm

Not to be picky, but this is a real bad antipattern:

Never throw Exception out of convenience. Either rethrow the checked exceptions you find in your method, or handle them as far as you can. Checked exceptions are to check for certain handeable error conditions. Simply throwing exception hinder you in that.

also:

is another one. Always close streams in a finally (yeah I know, it’s just an example, it’s just a file… but just do it on principle - it will save you hours of pulling your hair out when it comes to databases):


  BufferedReader reader = null;
  try
  {
        reader = new BufferedReader( new FileReader( new File(fileName) ) );
        String line;
        while( (line = reader.readLine()) != null ){
            text.add(line);
        }
  }
  finally
  {
        // always handle Exceptions in finally blocks
        // you don't want them to obscure an exception from the try block!
        try
        {
            if(reader!=null) reader.close();
        }
        catch(Exception ex)
        {
            System.err.println("Warning: couldn't close reader - " + ex.getMessage());
        }
  }

in most cases you cant handle checked exception usefull so just log the exception. This is just anoying for the coder because they reduce the readability of the code. I think its the best way to just rethrow the exception as an unchecked one.


  try {
      ...
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }

If you encounter an Exception, that you are sure cant be handled, you are right to throw a RuntimeException. But in most cases I would either let the checked ones buble up by declaring them in my method or rethrow a checked one like an IOException when handling any kind of IO.