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.