1、FileInputStream()
// 構建字節輸入流對象,參數為文件名 FileInputStream fin = new FileInputStream("message"); System.out.2、Randomaccess
File()String str6=""; ArrayList<String> strs=new ArrayList<String>(); RandomAccessFile file=new RandomAccessFile("message","r"); str6 = file.readLine(); while(str6!=null){ strs.add(str6); str6 = file.readLine(); } for(int j=0;j<strs.size();j++){ System.out.println(strs.get(j)); } file.close();3、File
File file2=new File("message"); if(file2.exists()&&file2.isFile()){ InputStream is=new FileInputStream(file2); byte[] buf = new byte[is.available()]; System.out.println(is.available()); while(is.read(buf)!=-1){ //每次讀取打印 System.out.println(new String(buf)); } }4、避免亂碼
File file3 = new File("message"); if (file3.exists() && file3.isFile()) { try { InputStream is = new FileInputStream(file3); InputStreamReader reader = new InputStreamReader(is,"utf-8");// char[] cbuf = new char[is.available()];//字符,字節 StringBuffer sb2=new StringBuffer();//2 while (reader.read(cbuf) != -1) { sb2.append(cbuf); } System.out.println(sb2.toString()); } catch (Exception e) { } }5、BufferedReader
File file = new File("message"); try { InputStream is = new FileInputStream(file); if (file.exists() && file.isFile()) { BufferedReader br = new BufferedReader( new InputStreamReader(is, "utf-8")); StringBuffer sb2 = new StringBuffer(); String line = null; while ((line = br.readLine()) != null) { sb2.append(line + "/n"); } br.close(); System.out.println(sb2.toString()); } } catch (Exception e) { }
新聞熱點
疑難解答