Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent Help

 
Himal Chuli
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic