Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can someone point me to some basic Java tutorials?  (Read 1095 times)
0 Members and 1 Guest are viewing this topic.
Offline Thsotus

Senior Newbie





« Posted 2010-10-10 04:50:01 »

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?

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline zoto

Senior Member


Medals: 4



« Reply #1 - Posted 2010-10-10 06:00:57 »

Here is a simple program to do that. Try checking the javaDoc for any of the classes you don't know.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
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();
        }
    }

}
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #2 - Posted 2010-10-10 06:25:00 »

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

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 43



« Reply #3 - Posted 2010-10-10 16:11:16 »

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

1  
    public TextTest(String fileName) throws Exception


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:

1  
2  
3  
4  
5  
6  
        BufferedReader reader = new BufferedReader( new FileReader( new File(fileName) ) );
        String line;
        while( (line = reader.readLine()) != null ){
            text.add(line);
        }
        reader.close();


is another one. <i>Always</i> 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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
  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());
        }
  }


Mathias - I Know What [you] Did Last Summer!
Offline OttoMeier

Senior Member


Medals: 4
Projects: 1



« Reply #4 - Posted 2010-10-10 20:53:02 »

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.
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.
1  
2  
3  
4  
5  
  try {
      ...
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }
Offline cylab

JGO Ninja


Medals: 43



« Reply #5 - Posted 2010-10-11 06:36:47 »

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.

Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!