Click to See Complete Forum and Search --> : java.io ?????
Dillinger4
Nov 19th, 2000, 01:16 PM
Can anyone show be a basic example of input out with Java.
I can do this in vb in two seconds with vb but with java
it seems to be a pain in the A%@....
import java.io.*;
class iotest{
public static void main(String[] args){
int chararacter;
byte[] hold;
FileDescriptor in = new FileDescriptor("C:\iotest");
FileInputStream fileinputstream = new FileInputStream(in);
while((character = FileInputStream.read(byte[] hold) != -1){
System.out.println("Reading Data");
}
}
}
Check out:
http://java.sun.com
http://java.sun.com/docs/books/tutorial/?frontpage-spotlight
http://java.sun.com/docs/books/tutorial/essential/io/index.html
http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html
The following Copy program uses FileReader and FileWriter to copy the contents of a file named farrago.txt into a file called outagain.txt:
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
By the way, the "Custom Networking" section will give you code to grab the html and you can put that in your parser (when complete) and render it and have your own browser that you can trust.
Dillinger4
Nov 19th, 2000, 04:27 PM
yeah i checked that block of code out yesterday, off of
Sun's site but they dont explain how to add in the
fullpath name ie....
File inputFile = new File("farrago.txt");
FileReader in = new FileReader(inputFile);
Let's say fargo.txt's path is C:\My Documents\fargo.txt
File inputFile = new File("C:\My Documents\fargo.txt");
FileReader in = new FileReader(inputFile);
Then if this is compiled it generates an illegal escape character error. I noticed the variables pathSeparator,
pathSeparatorChar,separatorChar but Sun fail's to
explain how to use them.
You almost had it. It found a "\" which is the signal to "escape" the next character's normal meaning. So you need "\\" where you had "\".
File inputFile = new File("C:\\My Documents\\fargo.txt");
The separator character idea would be usefull if you coded for different platforms. Unix would use "/" for directories instead of "\" like Dos/Win.
You have to watch that though because I think I've seen cases where if you put String s = new String("C:\My Documents\fargo.txt"); and then used File inputFile = new File(s);, it may not need the escape character like
String s = new String("C:\\My Documents\\fargo.txt");
It came up once or twice before whether or not you need to use the escape character. But keep that in mind.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.