This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes I/O and Streams and the fly likes Urgent  Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Urgent  Help" Watch "Urgent  Help" New topic
Author

Urgent Help

Himal Chuli
Ranch Hand

Joined: Aug 06, 2002
Posts: 118
Hi All,
I have the following class that reads a existing file with Java extension and prints 20 characters. I need a way to pause when each 20 characters are read and when pressed Enter, again read and print another 20 characters and so on. how do we do this. please give me some tips. sorry about my lack of knowledge in I/O.
import java.io.*;
public class TestReader {
public String askForFileName() {
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter File Name: ");
String fileName = bf.readLine();
return fileName.trim();
} catch (IOException io) {
System.out.println(io);
}
return null;
}
public boolean isFileJava(String fileName) {
return (fileName.substring(fileName.indexOf(".") + 1, fileName.length()).equalsIgnoreCase("java"));
}
public static void main(String[] args) throws IOException {
TestReader tr = new TestReader();
String file = tr.askForFileName().trim();
if (tr.isFileJava(file)) {
tr.readFile(file);
} else {
System.out.println("Not a java file");
}
}
public void readFile(String fileName) {
if (fileName != null) {
File f = new File(fileName);
try {
if (f.exists()) {
File fl = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(fl));
File outFile = new File("write.java");
PrintStream out = new PrintStream(new FileOutputStream(outFile));

System.out.println();
for (int i = 0; i < 20; i++) { System.out.print((char) br.read());
}
br.close();
out.close();
} else {
System.out.println("Does not exists.");
}
} catch (IOException io) {
System.out.println(io);
}
} else {
System.out.println("Wrong file.");
}
}
}
Himal


Himal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent Help