1、按字符读取
public String redConfigMSG() { String configMSG = ""; String path = "C:\\PrinterConfig\\BarCodePrinterConfig.txt"; try { int i; FileInputStream fis = new FileInputStream(path); while ((i = fis.read()) != -1) { configMSG += (char) i; } } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } return configMSG; }
2、按字符读取(一次读取一行)
public static void main(String[] args) { File file = new File("C:\\PrinterConfig\\BarCodePrinter.txt"); try { FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String str = bufferedReader.readLine(); while (str != null) { System.out.println("line:"+str); str = bufferedReader.readLine(); } } catch (FileNotFoundException e) { //当抛出多个异常时,子异常当在父异常前抛出。 e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }