Thursday, December 14, 2017

Java reading from console, reading from file

to read from console:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = bufferedReader.readLine();
Integer number = Integer.parseInt(line);
bufferedReader.close();


to read from file:

String fileName = "/path/to/a/file"
FileInputStream fileInputStream = new FileInputStream(fileName);
fileInputStream.available(); // byte count of a file
int byteValue = fileInputStream.read(); //reads bit from a file
char charValue = (char) byteValue; // convert byteValue to an actual symbol
Integer number = Character.getNumericValue(charValue);

while (fileInputStream.available() > 0)
{
System.out.println((char) fileInputStream.read());
}

fileInputStream.close()

No comments:

Post a Comment