Java Picture Book 3rd Edition Nine New Doors to Love Java https://www.amazon.co.jp/Java%E3%81%AE%E7%B5%B5%E6%9C%AC-%E7%AC%AC3%E7%89%88-Java%E3%81%8C%E5%A5%BD%E3%81%8D%E3%81%AB%E3%81%AA%E3%82%8B%E6%96%B0%E3%81%97%E3%81%849%E3%81%A4%E3%81%AE%E6%89%89-%E6%A0%AA%E5%BC%8F%E4%BC%9A%E7%A4%BE%E3%82%A2%E3%83%B3%E3%82%AF/dp/4798150371
ReadText.java
import java.io.FileReader;
import java.io.IOException;
public class ReadText {
public static void main(String[] args) {
try {
FileReader in = new FileReader(args[0]);
int c;
String s = new String();
System.out.println("****Display character integer as it is****");
while((c = in.read()) != -1) {
System.out.print(c); //Outputs integer information of characters
System.out.println();
s = s + (char)c; //Convert integer information to characters
}
System.out.print(s); //Output the character string converted to characters and combined
in.close();
} catch (IOException ie) { //Exception class representing I / O exceptions
System.out.println("No file");
} catch (Exception e) {
System.out.println("No file specified");
}
}
}
aiueo.txt
AIUEO
Kakikukeko
SA Shi Su Se So
A line break is also inserted after the last" so "
Execution result
****Display character integer as it is****
12354
12356
12358
12360
12362
10
12363
12365
12367
12369
12371
10
12373
12375
12377
12379
12381
10
AIUEO
Kakikukeko
SA Shi Su Se So
10 appears 3 times. It seems that the line break is 10 because there is a line break at the end of each line in aiueo.txt.
Maybe not only PHP but also Ruby can be written more easily.
However, since I have to describe the method of exchanging data with the text file one by one, I feel that I can see the contents and deepen my understanding.
Recommended Posts