LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-09-2008, 01:58 AM   #1
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Rep: Reputation: 50
learning java input output: tutorials for a confused student sought!


Background:
I'm getting back into university next year, so I'm working on a simple java program now, to freshen up my brain. (java being the language of the school.)*

Specifics:
The aspect I'm looking at right now is java input/output, specifically reading and writing a text file.

My problem is I can't understand the tutorials for I/O that I'm finding on google- this includes the Sun tutorials.

Please, can someone give me a good, easy to understand I/O tutorial, that is current and not depreciated (Java 5 or 6.)

I want to know:
  • How best to read a (multi line) file.
  • How best to write to a file.
  • How to add (write) to a file, leaving the old contents intact.

I learn from clearly commented code best. (with a little blurb explaining what that code is meant to do.)

[edit] Oh, and keep it simple. I don't want to deal with complex stuff that I don't need to know about yet. [/edit]

I'm not interested in buying books. Websites, please.

Thank you very much for your answers.

-titanium_geek

* I know java is a flawed language, but it's what I am required to work with, so please don't recommend I, learn C instead, for example.

Last edited by titanium_geek; 12-09-2008 at 02:06 AM. Reason: clarification, added details
 
Old 12-09-2008, 06:35 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by titanium_geek View Post
Background:
I'm getting back into university next year, so I'm working on a simple java program now, to freshen up my brain. (java being the language of the school.)*

Specifics:
The aspect I'm looking at right now is java input/output, specifically reading and writing a text file.

My problem is I can't understand the tutorials for I/O that I'm finding on google- this includes the Sun tutorials.

Please, can someone give me a good, easy to understand I/O tutorial, that is current and not depreciated (Java 5 or 6.)

I want to know:
  • How best to read a (multi line) file.
  • How best to write to a file.
  • How to add (write) to a file, leaving the old contents intact.

I learn from clearly commented code best. (with a little blurb explaining what that code is meant to do.)

[edit] Oh, and keep it simple. I don't want to deal with complex stuff that I don't need to know about yet. [/edit]

I'm not interested in buying books. Websites, please.

Thank you very much for your answers.

-titanium_geek

* I know java is a flawed language, but it's what I am required to work with, so please don't recommend I, learn C instead, for example.
First a disclaimer - I am not a Java guy.

Still, I think your post shows not confusion, but rather laziness. Or my mood is just bad ...

Anyway, the points are:
  • in Java everything is in classes;
  • classes have names and methods;
  • you just need first to identify the classes which deal with IO;
  • you then perform web search for the classes and their methods, there is a lot of Java code around, there is Google code search, so in the end you will find code, hopefully with comments

In Russian they are saying "(you not only want food), you also want it to be served on a beautiful plate" - an approximate translation - that's what I more or less felt reading your post.

And to get the knowledge you'll have to write code yourself, making mistakes and fixing them, asking yourself questions and figuring out answers - this is what learning about.

You, of course, can/may ask questions, but they should be specific, i.e., for example, you quote a manual page and point to the pieces which is not clear to you, and explain in detail what exactly is not clear, giving your possible interpretations and asking for clarifications.

Not just "I do not understand".
 
Old 12-09-2008, 06:50 AM   #3
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 129Reputation: 129
Too bad you are not interested in getting a book. I would strongly recommend E.R. Harold's "Java I/O" (O'Reilly), which is one of the best Java books I have ever read (from simple I/O to encryption, archiving, programming for hardware etc). It is all-inclusive, has lots of comments and extensive in-depth explanation, full source code illustrating everything, etc. Sure, you can figure it all out in bits and pieces and never gain an understanding of the fundamentals - but the book will serve these all up in a well-organized way in just 700 pages.

Last edited by jay73; 12-09-2008 at 06:51 AM.
 
Old 12-09-2008, 01:23 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
titanium_geek -

I completely sympathize with you. I've never understood from Java 1.0 why they never gave you a simple "printf" (or better, a FORTRAN or COBOL-style "write").

Anyway, here's a simple "Hello world":
http://www.cs.geneseo.edu/~baldwin/r...va-textio.html
Code:
    import java.io.*;

    class TextIOExample {

        public static void main( String args[] ) {

            try {

                BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
                System.out.print( "Destination file name = " );
                String fileName = in.readLine();
                PrintWriter out = new PrintWriter( new FileWriter( fileName ) );

                String textLine = in.readLine();
                while ( textLine != null && ! textLine.equals(".") ) {
                    out.println( textLine );
                    textLine = in.readLine();
                }

                out.close();
                in.close();
            }
            catch ( IOException error ) {
                System.err.println( "Error making file:" );
                System.err.println( "\t" + error );
            }
        }
    }
'Hope that helps .. PSM
 
Old 12-09-2008, 02:07 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by paulsm4 View Post
titanium_geek -

I completely sympathize with you. I've never understood from Java 1.0 why they never gave you a simple "printf" (or better, a FORTRAN or COBOL-style "write").

Anyway, here's a simple "Hello world":
http://www.cs.geneseo.edu/~baldwin/r...va-textio.html
Code:
    import java.io.*;

    class TextIOExample {

        public static void main( String args[] ) {

            try {

                BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
                System.out.print( "Destination file name = " );
                String fileName = in.readLine();
                PrintWriter out = new PrintWriter( new FileWriter( fileName ) );

                String textLine = in.readLine();
                while ( textLine != null && ! textLine.equals(".") ) {
                    out.println( textLine );
                    textLine = in.readLine();
                }

                out.close();
                in.close();
            }
            catch ( IOException error ) {
                System.err.println( "Error making file:" );
                System.err.println( "\t" + error );
            }
        }
    }
'Hope that helps .. PSM
Java already has printf - which made C/C++ proponents sarcastically laugh - because Java originally didn't have it.

Here is a link: http://www.particle.kth.se/~lindsey/...terPrintf.html .

By the way, http://www.particle.kth.se/~lindsey/JavaCourse/Book appears to be a book on Java.

Java is notorious for unnecessary length of code it requires to write...
 
Old 12-09-2008, 07:00 PM   #6
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Original Poster
Rep: Reputation: 50
I responded to the above, particularly Sergei's post, in an emotional, angry way. I have removed it so I can respond in a more thoughtful way later.

Last edited by titanium_geek; 12-09-2008 at 07:32 PM. Reason: remove flame bait
 
Old 12-10-2008, 02:47 AM   #7
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Original Poster
Rep: Reputation: 50
OK: what I would like, is for someone to tell me what java (5 or 6) classes are used for input/output. Then I can read up about them and work out how to use them.

I'm trying to be vague because in the past, I've asked questions with specific information about the program I was trying to write, and someone wrote the whole program for me, which isn't quite helpful when I'm trying to learn how to do it myself.

TG
 
Old 12-10-2008, 02:58 AM   #8
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 129Reputation: 129
Start by inspecting the InputStream and OutputStream classes. They are unusable as they are abstract but they are the basis of nearly all other byte stream classes, i.e. their methods are shared by all those classes. The same goes for the Reader and Writer classes, which are the basis of nearly all character streams.
The truth is that I/O is extremely easy - once you get the fundamentals right (just a handful of methods and principles), you can work out the rest from there.

Last edited by jay73; 12-10-2008 at 03:05 AM.
 
Old 12-10-2008, 05:34 AM   #9
arunmathew1984
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
You should try buying/downloading the book called "Thinking In Java" by "Bruce Eckel". Its one of the best books you can learn from.

It's best for novices as well as advanced developers!


Linux Archive

Last edited by arunmathew1984; 12-20-2008 at 11:16 AM.
 
Old 12-10-2008, 11:27 PM   #10
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, titanium_geek -

I completely agree with arunmathew1984's recommendation for Bruce Eckel's classic "Thinking in Java". My Third Edition copy is well-worn and dog-eared, the current Fourth Edition covers Java 6. It's a great primer for the novice; it's a great reference for the practitioner.

And I apologize for Sergei's completely unwarranted reponse to your original question. You've been around LQ long enough to know that's not the norm, but I'm sorry that it happened.

IMHO .. PSM

PS:
Bruce Eckel's home page ... and the free download of his 3rd Edition (which I can personally assure you is still an excellent, useful reference) is here:
http://mindview.net/

Last edited by paulsm4; 12-10-2008 at 11:29 PM.
 
  


Reply

Tags
input, java, output, tutorial


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
student + major project + internship + future + suggestions + confused + help koodoo General 10 09-21-2007 04:16 PM
LTSP up n running .. now I need to configure (input sought) freestyle Linux - Software 0 05-08-2006 02:05 PM
simple input output java 1.4 titanium_geek Programming 4 05-07-2006 06:33 AM
Advice sought RE Input/output errors during backup attempt conn-fused Linux - Software 2 12-17-2005 01:25 AM
Advice sought RE input/output errors during backup attempt conn-fused Linux - Hardware 0 12-16-2005 05:42 AM


All times are GMT -5. The time now is 08:03 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration