国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

Java—從文件中讀取數(shù)據(jù)

2019-11-14 21:07:01
字體:
供稿:網(wǎng)友
java—從文件中讀取數(shù)據(jù)

1、FileInputStream()

              // 構(gòu)建字節(jié)輸入流對象,參數(shù)為文件名              FileInputStream fin = new FileInputStream("message");              System.out.

2、RandomaccessFile()

              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()];//字符,字節(jié)                                        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) {            }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湖南省| 桂林市| 墨竹工卡县| 资源县| 和田县| 清新县| 仁布县| 旬阳县| 丰台区| 奎屯市| 澳门| 枝江市| 桦川县| 华池县| 唐海县| 姚安县| 滕州市| 互助| 高碑店市| 鲁山县| 洛浦县| 稷山县| 婺源县| 海城市| 泌阳县| 平南县| 什邡市| 尼木县| 济源市| 绥棱县| 集安市| 伊宁市| 珲春市| 安阳市| 浦东新区| 双牌县| 荣成市| 资阳市| 建始县| 黄平县| 临朐县|