Discussions
Categories
Groups
Advertisement
Child Item
Home
Topics
Technology & Internet
Software & Web Development
Development
Damn Java
seamus
The wonderful heads here in UCD decided it would be better to teach us how to code and about algorithms, than about how to interact with the system, so as a result, I can code like a maniac in Java, but can't write anything half useful. Frankly, I suck.
So two, little nigglers.....
I have an open connection to an Http server, and read a requested document into an array of strings. Simple enough.
Now I want to output this array to a file.
So I create a FileWriter object with the name of the file (I assume this means it will write the file to the same dir as the .class file).
But when I try to begin writing the array, using a for-loop and calling
write(s
)
, it keeps throwing NullPointerException, and frankly I have no idea what's going on.
So, how do I write and array of strings to a file, or better still, how can I connect an InputStream to an OutputStream, effectively bypassing the need to use a clumsy String array at all?
And niggler no. 2.
I need to be able to take some arguments at the command line, ie
java MyClass.java arg1 arg2 arg3........
How do I reference and passed arguments?
Thanking you.......
Find more posts tagged with
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
Advertisement
Advertisement
Comments
ZeFrog
hi,
I ve never tried that myself but try to pass a reference to your array in the write method instead of a loop that write every element.
Something like write(s) instead of write(s
)
Also in the API it seems that the write method is designed that way :
public void write(char[] cbuf,
int off,
int len)
throws IOException
Can you use an array of char instead of an array of string ? And then pass the reference to the write method .
try 0 for the off parameter, and s.length for len
for n2
If you type in java MyClass arg1 arg2 it will execute an application named MyClass. This app has a main method :
public static void main( String args[] ) {}
args[0] will give you arg1 (as a string)
args[1] will give you arg2 and so on
So let's say you want to output the 2 arguments :
public static void main( String args[] ) {
String output ;
output = ""+args[0]+args[1];
System.out.print(output);
}
On the command line if you type java MyClass Hello all
it will output Hello all
hope this help a bit.
seamus
Nice one. The args bit definitely helps, but...
I tried converting each string to an array of char as you suggested, but I kept getting the same NullPointerException. I'll have another look at what is actually retrieved from the server, maybe, there is some null input creeping in there......
ZeFrog
Dear Seamus,
I have tried a little program based on a String converted to an array of char. It does create a file in the current directory as you wish. How ever it doesn t use a FileWriter but a RandomAccessFile instead. Have a look anyway.
import java.io.*;
public class MyFile {
public static void main( String[] args ) {
try {
String str="Hi everybody";
char[] cArray = str.toCharArray();
RandomAccessFile f = new RandomAccessFile("aFile.txt", "rw");
for ( int i = 0; i < cArray.length; i++ ) {
f.writeChar(cArray
);
}//end for
f.close();
} //end try block
catch ( Exception e ) {}
}//end main
}//end class
seamus
Nice one cheers. I'll have a look at that RandomAccessFile tonight. I'm also thinking that because I'll be looping through the String array, I'm going to need to set append to 1........
satchmo
PrintWriter fileOut=new PrintWriter(new FileOutputStream("filename.txt",true));
for(int s=0;s<strings.length;s++)fileOut.println(strings
);
That
true
is for appending.
Problem solved.
ZeFrog
Inspired by Jazz I wrote this, it allows u to append new Strings to the file :
import java.io.*;
public class MyFile {
public static void main( String[] args ) {
String[] all = {"Hi", "Bye"};
try{
FileWriter fileOut = new FileWriter("file.txt", true);
for(int i=0;s<all.length;i++){fileOut.write(all
);}
fileOut.close();
}
catch(Exception e){}
}//end main
}//end class
For the RandomAccessFile, you need to place the pointer at the end of the file to append :
f.seek( f.length() );
seamus
I don't seem to have the RandomAccessFile class, probably time for some upgradage.
Anyway got it all sorted cheers. I replaced the String array with a Vector - I was getting NullPointerException's because of unused cells in the array, and then just created one big long String to be outputted to the file. Thanks for your help.
Project finished. Yay!