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
| FAQ | Members List | 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


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.

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);

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

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


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

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

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

         if (fileIn.exists())
  // put data to a Disk File
  static void ToFile(String thefilename, DataOutputStream aStream) 

    this.aStream = new DataOutputStream(aStream);

    System.out.println("Data would be written to file Now! " +thefilename);

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


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
    this.aStream = new DataOutputStream(aStream);
mysimpleio.java:66: package this does not exist
mysimpleio.java:75: non-static variable this cannot be referenced from a static
    this.aStream = new DataInputStream(aStream);
mysimpleio.java:76: package this does not exist
4 errors


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.



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 08:09 PM.

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