参考1:
参考2:
文本文档位于工程下,使用鼠标右键点击工程,选择new -> File,即可创建。
文本文档的格式:GBK
例1:单字节读取
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 public class Main { 8 9 public static void main(String[] args) {10 System.out.println(System.getProperty("user.dir"));11 12 File file = new File("text.txt");13 InputStream inputStream = null;14 15 try {16 if ((file.exists()) && (file.isFile())) {17 inputStream = new FileInputStream(file);18 int data = -1;19 do {20 data = inputStream.read();21 if (data != -1) {22 System.out.print((char) data);23 } else {24 System.out.print(data);25 }26 } while (data != -1);27 System.out.println();28 } else if (!file.exists()) {29 System.out.println("The " + file.getName() + " does not exist.");30 } else if (!file.isFile()) {31 System.out.println("The " + file.getName() + " is not a file.");32 }33 } catch (FileNotFoundException e) {34 e.printStackTrace();35 } catch (IOException e) {36 e.printStackTrace();37 } finally {38 try {39 if (inputStream != null) {40 // Closes this input stream and releases any system resources associated with the stream.41 inputStream.close();42 System.out.println("Close the input stream.");43 }44 } catch (IOException e) {45 e.printStackTrace();46 }47 }48 }49 }
改写例1:
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 public class Main { 8 9 public static void main(String[] args) {10 System.out.println(System.getProperty("user.dir"));11 12 File file = new File("text.txt");13 14 try (InputStream inputStream = new FileInputStream(file)) {15 if ((file.exists()) && (file.isFile())) {16 int data = -1;17 do {18 data = inputStream.read();19 if (data != -1) {20 System.out.print((char) data);21 } else {22 System.out.print(data);23 }24 } while (data != -1);25 System.out.println();26 } else if (!file.exists()) {27 System.out.println("The " + file.getName() + " does not exist.");28 } else if (!file.isFile()) {29 System.out.println("The " + file.getName() + " is not a file.");30 }31 } catch (FileNotFoundException e) {32 e.printStackTrace();33 } catch (IOException e1) {34 e1.printStackTrace();35 }36 }37 }
多字节读取
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 public class IOTest02 { 8 9 public static void main(String[] args) {10 File src = new File("src.txt");11 InputStream is = null;12 13 try {14 is = new FileInputStream(src);15 byte[] buffer = new byte[1024 * 1]; // 1k bytes16 int length = -1;17 while ((length = is.read(buffer)) != -1) {18 String str = new String(buffer, 0, length); // decode19 System.out.print(str);20 }21 } catch (FileNotFoundException e) {22 e.printStackTrace();23 } catch (IOException e) {24 e.printStackTrace();25 } finally {26 try {27 if (is != null) {28 is.close();29 System.out.println("\n\nInputStream Closed.");30 }31 } catch (IOException e) {32 e.printStackTrace();33 }34 }35 }36 }