一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java讀寫文件方法總結(推薦)

Java讀寫文件方法總結(推薦)

2020-05-08 11:33jingxian JAVA教程

下面小編就為大家帶來一篇Java讀寫文件方法總結(推薦)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Java讀寫文件方法在工作中相信有很多的用處的,本人在之前包括現在都在使用Java的讀寫文件方法來處理數據方面的輸入輸出,確實很方便。奈何我的記性實在是叫人著急,很多時候既然都會想不起來怎么寫了,不過我的Java代碼量也實在是少的可憐,所以應該多多練習。這里做一個總結,集中在一起方面今后查看。

Java讀文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package 天才白癡夢;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
 
public class JavaIO {
  
  /**
   * 采用的是操作系統底層默認的編碼方式,GBK等,非UTF8
   * */
  
  /**
   * 以字節為單位讀取文件內容,常用于讀取二進制文件,比如圖片、影像、聲音等文件
   * */
  public static void readFileByBytes(String filename) {
    File file=new File(filename);
    FileInputStream in=null;
    try {
      System.out.println("以字節為單位讀取文件,一次讀一個字節: ");
      in=new FileInputStream(file);
      int temp=0;
      while ((temp=in.read()) != -1) {
        System.out.println(temp);
      }
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
      return ;
    }
    try {
      System.out.println("以字節為單位讀取文件,一次讀多個字節: ");
      byte[] temp=new byte[100];
      int byteread=0;
      in=new FileInputStream(file);
      JavaIO.showAvailableBytes(in);
      while ((byteread=in.read(temp)) != -1) {
        System.out.write(temp,0,byteread);
      }
    } catch (Exception e1) {
      e1.printStackTrace();
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e1) {
          
        }
      }
    }
  }
  /**
   * 以字符為單位讀取文件,常用于讀文本,數字等類型的文件
   * */
  public static void readFileByChar(String filename) {
    File file=new File(filename);
    Reader reader=null;
    try {
      System.out.println("以字符為單位讀取文件內容,一次一個字節:");
      //InputStreamReader類:是字節向字符轉換的橋梁
      reader=new InputStreamReader(new FileInputStream(file));
      int temp;
      while ((temp=reader.read()) != -1) {
        if (((char)temp) != '\r') {
          System.out.println((char)temp);
        }
      }
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      System.out.println("以字符為單位讀取文件內容,一次讀多個字節: ");
      char[] temp=new char[30];
      int charread=0;
      reader=new InputStreamReader(new FileInputStream(filename));
      while ((charread=reader.read(temp)) != -1) {
        if ((charread == temp.length) && (temp[temp.length-1]!='\r')) {
          System.out.println(temp);
        } else {
          for (int i=0; i<charread; i++) {
            if (temp[i] == '\r') {
              break;
            } else {
              System.out.println(temp[i]);
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          
        }
      }
    }
  }
  /**
   * 以行為單位讀取文件,常用于讀面向行的格式化文件
   * */
  public static void readFileByLine(String filename) {
    File file=new File(filename);
    BufferedReader reader=null;
    try {
      System.out.println("以行為單位讀取文件內容,一次讀一整行: ");
      reader=new BufferedReader(new FileReader(file));
      String temp=null;
      int line=1;
      while ((temp=reader.readLine()) != null) {
        System.out.println("line " + line + ": " + temp);
        line++;
      }
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          
        }
      }
    }
  }
  /**
   * 隨機讀取文件內容
   * */
  public static void readFileByRandomAccess(String filename) {
    RandomAccessFile randomfile=null;
    try {
      System.out.println("隨機讀取一段文件內容");
      randomfile=new RandomAccessFile(filename,"r");
      long fileLength=randomfile.length();
      int beginIndex=(fileLength > 4 ? 4 : 0);
      randomfile.seek(beginIndex);
      byte[] bytes=new byte[10];
      int byteread=0;
      while ((byteread=randomfile.read(bytes)) != -1) {
        System.out.write(bytes,0,byteread);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (randomfile != null) {
        try {
          randomfile.close();
        } catch (IOException e) {
          
        }
      }
    }
  }
  private static void showAvailableBytes(InputStream in) {
    try {
      System.out.println("當前字節輸入流中的字節數為:" + in.available());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {
    String filename="E:\\BaiYiShaoNian.txt";
    JavaIO.readFileByBytes(filename);
    JavaIO.readFileByChar(filename);
    JavaIO.readFileByLine(filename);
    JavaIO.readFileByRandomAccess(filename);
  }
}

Java寫文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package 天才白癡夢;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
 
public class JavaIO2 {
 
  public static void main(String[] args) throws IOException {
    String Path="E:\\天才白癡夢\\JAVA";
    File file=new File("E:\\天才白癡夢\\JAVA","BaiYiShaoNian.txt");
    if (!file.exists()) {
      try {
        file.createNewFile();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    /**
     * Java寫入文件的三種方法
     * */
    FileOutputStream fos=null;
    BufferedWriter bw=null;
    FileWriter fw=null;
    int value=1000;
    
    try {
      fos=new FileOutputStream(new File(Path+"fos.txt"));
      long begin=System.currentTimeMillis();
      for (int i=1; i<=value; i++) {
        fos.write(5);
      }
      long end=System.currentTimeMillis();
      System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));
      fos.close();
      
      bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));
      begin=System.currentTimeMillis();
      for (int i=1; i<=value; i++) {
        bw.write(5);
        bw.newLine();
      }
      bw.close();
      end=System.currentTimeMillis();
      System.out.println("TheCostTime of BufferedWriter is : " + (end-begin));
      
      fw=new FileWriter(Path+"fw.txt");
      begin=System.currentTimeMillis();
      for (int i=1; i<=value; i++) {
        fw.write(5);       
      }
      fw.close();
      end=System.currentTimeMillis();
      System.out.println("TheCostTime of FileWriter is : " + (end-begin));
      
      
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        fos.close(); //FileOutputStream
        bw.close(); //BufferedWriter
        fw.close(); //FileWriter
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    
  }
}

Java讀寫文件方法總結(推薦)

以上這篇Java讀寫文件方法總結(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 石原莉奈adn093店长未婚妻 | 999久久免费高清热精品 | 四虎视屏| 国内精品伊人久久大香线焦 | 亚洲色大成网站www久久九九 | 免费在线观看亚洲 | 日本tube24xxxxx | 精品无人区乱码1区2区3区免费 | 四虎在线永久视频观看 | 青草色视频 | 日本69sex护士www | 男人女人日皮视频 | 天堂色| 日本色女 | 欧美日韩国产手机在线观看视频 | 男男羞羞视频网站国产 | 成人在线一区二区三区 | 成人在线观看一区 | 亚洲国产日韩欧美一区二区三区 | 福利片福利一区二区三区 | 欧美日韩国产在线一区 | 国产精品色图 | gay小太正初精 | 亚洲精品中文字幕第一区 | 美女被草逼 | 欧美精品一区二区三区免费 | 午夜视频在线网站 | 亚洲男人天堂久久 | 日韩精品福利视频一区二区三区 | 国产人妖ts在线视频网 | 日本一片免费观看高清完整 | 关晓彤被调教出奶水的视频 | 国产精品精品 | 欧美特黄视频在线观看 | 含羞草传媒每天免费一次破解 | 国产v视频 | 精品午夜久久福利大片免费 | 95视频在线观看在线分类h片 | 性满足久久久久久久久 | 国产精品露脸国语对白99 | 国产精品久久国产三级国电话系列 |