Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 8th, 2007, 04:38 AM
Authorized User
 
Join Date: Feb 2005
Location: chch, n/a, New Zealand.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing DataOuputStr/DataInputStream to a method

Got a console app and i would like to get to grips with file I/O operations.

what i have done is to start with a simple program that takes a parameter in at the command line and prints it to the screen

eg

c:\> mysimpleio test

program reprints "test" - easy peasy :D even added validation code so if you didn't have the word test at the end it just displayed a message!.

Now i'm wanting to write the text to a file and read it back.

it is this bit i am having trouble with.

I have 2 methods 1 called tofile and another called fromfile.
i used these in another version to try and help me break the program up into smaller chucks.

my complete program code is below.


Code:
import java.io.*;

public class mysimpleio
{
  String thefilename = "";

  DataOutputStream thefileOut = null;
  DataInputStream thefileIn = null;

  public static void main(String[] args)
  {
    String text = "";
    String path = "e:\\my_project\\crm\\";
    if (args.length == 0)
      {         
        System.out.println("No file name, Exiting program!");
      }
    if (args.length > 0)
      {
        for (int i=0; i < args.length; i++)
          text = text + args[i];


          System.out.print (path);


         File fileOut = new File (path,text);
         fileOut.createNewFile();

          DataOutputStream thefileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileOut)));

        try
        {
          if (fileOut.exists())
            fileOut.delete();
        }
        catch(IOException e)
        {
         System.out.print("Error occured!"+ e);
        }

        ToFile(text,thefileOut);
        thefileOut.close();

        System.out.print("\n okay so you entered "+ text);

        File fileIn = new File (path,text);
        fileIn.createNewFile();
          DataInputStream thefileIn = new DataInputStream(new BufferedInputStream(new FileInputStream(fileIn)));


        if (!fileIn.exists())
           System.out.println("Sorry no file found matching " + text);

        else
         if (fileIn.exists())
           FromFile(text,thefileIn);
           thefileIn.close();
      }
  }
  // put data to a Disk File
  static void ToFile(String thefilename, DataOutputStream aStream) 
  {

    this.aStream = new DataOutputStream(aStream);
    this.aStream.writeUTF(thefilename);

    System.out.println("Data would be written to file Now! " +thefilename);
    //this.aStream.close;
  }

  //Read data From a Disk File
  static void FromFile(String thefilename,DataInputStream aStream) 
  {
    this.aStream = new DataInputStream(aStream);
    this.aStream.readUTF(thefilename);
    System.out.println(" Data would be Read From file Now! " +thefilename);
    //this.aStream.close;

  }


}
errors i am getting are here

E:\my_project\crm>javac mysimpleio.java
mysimpleio.java:65: non-static variable this cannot be referenced from a static
context
    this.aStream = new DataOutputStream(aStream);
    ^
mysimpleio.java:66: package this does not exist
    this.aStream.writeUTF(thefilename);
    ^
mysimpleio.java:75: non-static variable this cannot be referenced from a static
context
    this.aStream = new DataInputStream(aStream);
    ^
mysimpleio.java:76: package this does not exist
    this.aStream.readUTF(thefilename);
    ^
4 errors

E:\my_project\crm>


I have tried al sorts of things to no avail.

I have come a long way in doing this but the book is a bit vauge to me on passing or using objects in a method.:(

Really tho i am sure i'm missing something _very_ basic and would like help on this.

dave.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
adding value and passing to method gantait XSLT 2 February 20th, 2007 04:45 AM
Why is this method placed in filip BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 August 25th, 2006 09:40 PM
best method for passing variables jamara VBScript 2 March 29th, 2005 05:26 PM
Method StanArtis BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 December 6th, 2004 06:09 AM
Access Forms Open Method of Passing Parameters androman Access VBA 8 September 22nd, 2004 09:04 AM



All times are GMT -4. The time now is 09:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.