Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File I/O ... Reading and Writing to a file
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kuntzy




PostPosted: Tue Sep 28, 2004 9:55 am   Post subject: File I/O ... Reading and Writing to a file

Can anyone plz give me a tutorial on reading and writing to a file in java console? I searched the tutorials in java but i couldn't find what I need ... maybe sum example like opening a file with numbers in it and modifying them.
Sponsor
Sponsor
Sponsor
sponsor
.hack




PostPosted: Thu Oct 14, 2004 8:35 am   Post subject: (No subject)

I second this motion. My Programming taecher doesn't even know Java yet some how he got a job teaching it. So his way of teaching us is telling us what to read out of thise POS holtsoft book and learn it that way using "Ready To Java" (Actually named, "Wasting Your Time").

Anyways the book has l9ike 4 pages on IO except the code they give you doesn't work because this book was made by a group of tools. Example.

code:

// The "OddInts" class.
import java.awt.*;
import hsa.Console;
import java.io.*;

public class OddInts
{
    static Console c;           // The output console
   
   
    public static void main (String[] args)
    {
        c = new Console ();
       

        PrintWriter output;
        String fileName;
       
       
        c.println ("What is the name of the file for the integers?");
        fileName = c.readLine();
        output = new PrintWriter (new FileWriter (fileName));
        for (int number = 1;number <=361;number +=2)
        {
        output.println (number);
        }
        //CLose file
        output.close();
    } // main method
} // OddInts class


Now, is the book they don't TELL YOU that you need to import Java.io and they tell you that youneed to enter "throws IOException" in there somewhere, but it doesn't tell you where. God help us. or COmpsci Razz
wtd




PostPosted: Thu Oct 14, 2004 2:33 pm   Post subject: (No subject)

If you're frequently doing complex, specialized file I/O, it can be useful to extend the classes in question and add methods which do what you want.

An example from this thread:

code:
import java.lang.*;
import java.io.*;
import java.text.*;

class MoneyReader extends BufferedReader {
   public MoneyReader(Reader in) {
      super(in);
   }

   public double getMonetaryAmount() throws IOException {
      return Double.parseDouble(readLine());
   }
}


A new MoneyReader was created by:

code:
private static MoneyReader keyboard = new MoneyReader(new InputStreamReader(System.in));


And I used its functionality with:

code:
double price = keyboard.getMonetaryAmount();
Hikaru79




PostPosted: Sat Oct 16, 2004 3:57 pm   Post subject: (No subject)

Here's a clue Very Happy You'll have to figure the rest out by yourself:

code:

import java.awt.*;
import java.io.*;

public class Foo
{

    public static void main (String [] args)
        throws IOException
    {

fileName = "test.txt";
BufferedReader input;
input = new BufferedReader (new FileReader (fileName));
line = input.readLine ();
System.out.println (line);
     }
}


That will work if you use it correctly Smile I leave the rest to you!
wtd




PostPosted: Sat Oct 16, 2004 6:01 pm   Post subject: (No subject)

Hikaru79 wrote:
Here's a clue Very Happy You'll have to figure the rest out by yourself:

code:

import java.awt.*;
import java.io.*;

public class Foo
{

    public static void main (String [] args)
        throws IOException
    {

fileName = "test.txt";
BufferedReader input;
input = new BufferedReader (new FileReader (fileName));
line = input.readLine ();
System.out.println (line);
     }
}


That will work if you use it correctly Smile I leave the rest to you!


That won't compile. You haven't declared the variables "fileName" or "line". Additionally, you didn't import java.lang, but you did import java.awt, which doesn't do anything in this sample.

code:
F:\Programming>javac Foo.java
Foo.java:11: cannot find symbol
symbol  : variable fileName
location: class Foo
fileName = "test.txt";
^
Foo.java:13: cannot find symbol
symbol  : variable fileName
location: class Foo
input = new BufferedReader (new FileReader (fileName));
                                            ^
Foo.java:14: cannot find symbol
symbol  : variable line
location: class Foo
line = input.readLine ();
^
Foo.java:15: cannot find symbol
symbol  : variable line
location: class Foo
System.out.println (line);
                    ^
4 errors


Instead, this could be rewritten:

code:
import java.lang.*;
import java.io.*;

public class Foo {
   public static void main(String[] args) throws IOException {
      String fileName = "test.txt";
      BufferedReader input = new BufferedReader(new FileReader(fileName));
      String line = input.readLine();
      System.out.println(line);
   }
}
Hikaru79




PostPosted: Sat Oct 16, 2004 10:51 pm   Post subject: (No subject)

Oh, my bad about the java.awt.* . Embarassed As for the missing variable declarations, I know they're missing. This wasn't meant to compile, just to be a rough framework of how I/O works without hsa.
wtd




PostPosted: Sat Oct 16, 2004 11:13 pm   Post subject: (No subject)

It's ok. I just find it useful, when providing assistance, if you're going to provide code, if you can test it, you really should.

There's a tendency for students who know nothing to assume that they did something wrong when they can't compile code provided by someone they perceive as more knowledgeable.
Hikaru79




PostPosted: Sun Oct 17, 2004 9:26 am   Post subject: (No subject)

wtd wrote:
It's ok. I just find it useful, when providing assistance, if you're going to provide code, if you can test it, you really should.

There's a tendency for students who know nothing to assume that they did something wrong when they can't compile code provided by someone they perceive as more knowledgeable.


I never thought of it that way =/ I was just thinking it was a way to at least make them read the code ^ ^;; See what's missing.

But you're right. I'll be careful next time Smile
Sponsor
Sponsor
Sponsor
sponsor
Kuntzy




PostPosted: Mon Oct 18, 2004 9:53 pm   Post subject: (No subject)

Well i got the ready to program version of file IO working ... too bad its not real Java ...
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: