大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java文件讀寫的方法

Java文件讀寫的方法

更新時間:2022-09-07 10:11:49 來源:動力節點 瀏覽1271次

Java教程中,Java文件讀寫的方法是最基礎的課程。

使用 FileReader 和 FileWriter 讀寫

讓我們從使用FileReader和FileWriter類開始:

String directory = System.getProperty("user.home");
String fileName = "sample.txt";
String absolutePath = directory + File.separator + fileName;
// Write the content in file 
try(FileWriter fileWriter = new FileWriter(absolutePath)) {
    String fileContent = "This is a sample text.";
    fileWriter.write(fileContent);
    fileWriter.close();
} catch (IOException e) {
    // Cxception handling
}
// Read the content from file
try(FileReader fileReader = new FileReader(absolutePath)) {
    int ch = fileReader.read();
    while(ch != -1) {
        System.out.print((char)ch);
        fileReader.close();
    }
} catch (FileNotFoundException e) {
    // Exception handling
} catch (IOException e) {
    // Exception handling
}

這兩個類都接受一個字符串,在它們的構造函數中表示文件的路徑。您還可以傳遞一個File對象以及一個FileDescriptor.

該write()方法寫入一個有效的字符序列 - a String, a char[]。此外,它可以編寫一個char表示為int.

該read()方法逐個字符地讀取并返回,while例如,允許我們在循環中使用讀取的數據。

不要忘記在使用后關閉這兩個類!

使用 BufferedReader 和 BufferedWriter 讀寫

使用BufferedReader和BufferedWriter類:

String directory = System.getProperty("user.home");
String fileName = "sample.txt";
String absolutePath = directory + File.separator + fileName;
// Write the content in file 
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(absolutePath))) {
    String fileContent = "This is a sample text.";
    bufferedWriter.write(fileContent);
} catch (IOException e) {
    // Exception handling
}
// Read the content from file
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(absolutePath))) {
    String line = bufferedReader.readLine();
    while(line != null) {
        System.out.println(line);
        line = bufferedReader.readLine();
    }
} catch (FileNotFoundException e) {
    // Exception handling
} catch (IOException e) {
    // Exception handling
}

使用 FileInputStream 和 FileOutputStream 讀寫

使用FileInputStream和FileOutputStream類:

String directory = System.getProperty("user.home");
String fileName = "sample.txt";
String absolutePath = directory + File.separator + fileName;
// write the content in file 
try(FileOutputStream fileOutputStream = new FileOutputStream(absolutePath)) {
    String fileContent = "This is a sample text.";
    fileOutputStream.write(fileContent.getBytes());
} catch (FileNotFoundException e) {
    // exception handling
} catch (IOException e) {
    // exception handling
}
// reading the content of file
try(FileInputStream fileInputStream = new FileInputStream(absolutePath)) {
    int ch = fileInputStream.read();
    while(ch != -1) {
        System.out.print((char)ch);
        ch = fileInputStream.read();
    }
} catch (FileNotFoundException e) {
    // exception handling
} catch (IOException e) {
    // exception handling
}

使用 BufferedInputStream 和 BufferedOutputStream 進行讀寫

使用BufferedInputStream和BufferedOutputStream類:

String directory = System.getProperty("user.home");
String fileName = "sample.txt";
String absolutePath = directory + File.separator + fileName;
// write the content in file 
try(BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(absolutePath))) {
    String fileContent = "This is a sample text.";
    bufferedOutputStream.write(fileContent.getBytes());
} catch (IOException e) {
    // exception handling
}
// read the content from file
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(absolutePath))) {
    int ch = bufferedInputStream.read();
    while(ch != -1) {
        System.out.print((char)ch);
        ch = bufferedInputStream.read();
    }
} catch (FileNotFoundException e) {
    // exception handling
} catch (IOException e) {
    // exception handling
}

使用 Java.nio 類讀寫

使用java.nio類:

String directory = System.getProperty("user.home");
String fileName = "sample.txt";
String content = "This is a sample text.";
Path path = Paths.get(directory, fileName);
try {
    Files.write(path, content.getBytes(), StandardOpenOption.CREATE);
} catch (IOException e) {
    // exception handling
}
try {
    List<String> list = Files.readAllLines(path);
    list.forEach(line -> System.out.println(line));
} catch (IOException e) {
    // exception handling
}

另一種通過Files類檢索內容的方法(如果您不讀取文本數據則更重要)是使用該readAllBytes方法將數據讀入字節數組:

try { 
    byte[] data = Files.readAllBytes(path);
    System.out.println(new String(data));
} catch (IOException e) {
    // exception handling
}

如果您有興趣使用帶有的流java.nio,您還可以使用該類提供的以下方法Files:

Files.newBufferedReader(path)
Files.newBufferedWriter(path, options)
Files.newInputStream(path, options)
Files.newOutputStream(path, options)

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 手机看片久久 | 精品国精品国产自在久国产应用 | 91热久久免费频精品动漫99 | 成人免费黄网站 | 国产精品夜色视频一级区 | 欧美日韩成人午夜免费 | 中国一级毛片欧美一级毛片 | 久久精品最新免费国产成人 | 国产99免费视频 | 日韩欧美国产一区二区三区四区 | 亚洲精品久久久中文字 | 99国产精品九九视频免费看 | 国产亚洲第一伦理第一区 | 色狠狠一区二区三区香蕉蜜桃 | 四虎在线观看免费视频 | 深夜影院a | 狠狠色丁香婷婷久久综合考虑 | 青草免费 | 在线久草视频 | 91精品成人免费国产片 | 欧美日韩国产最新一区二区 | 亚洲一级毛片免费观看 | 日日夜夜拍| 国产v欧美v日韩在线观看 | 国产成人久久久精品一区二区三区 | 最新国产精品亚洲二区 | 四虎影院紧急入口 | 五月婷婷激情视频 | 在线观看黄色网 | 一男一女一级毛片 | 久久免费视频在线观看30 | 色日韩在线 | 久草在线视频在线 | 深夜成人在线 | 日本一区二区三区不卡在线视频 | 国产色网 | 一区二区三区欧美在线 | 成人午夜视频在线播放 | 免费看爱爱视频 | 四虎国产精品永久在线看 | 欧美又粗又硬 |