黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) 學(xué)習(xí)攻略 Java學(xué)習(xí) Java文件讀寫的方法

Java文件讀寫的方法

更新時(shí)間:2022-09-07 10:11:49 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1293次

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

使用 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
}

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

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

該read()方法逐個(gè)字符地讀取并返回,while例如,允許我們?cè)谘h(huán)中使用讀取的數(shù)據(jù)。

不要忘記在使用后關(guān)閉這兩個(gè)類!

使用 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 進(jìn)行讀寫

使用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
}

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

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)

 

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-05-15 搶座中
  • 全國(guó)校區(qū) 2025-06-05 搶座中
  • 全國(guó)校區(qū) 2025-06-26 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 亚久久伊人精品青青草原2020 | 波多野一区二区 | 日韩福利视频高清免费看 | 狠狠澡夜夜澡人人爽 | a免费网站| 国产成人一区二区三区在线播放 | 第一福利官方导航大全 | 99插插| 毛片网站有哪些 | 人人澡人摸人人爽歪歪 | 日韩精品1区 | 日韩一级二级三级 | 欧美第九页 | 天天爱夜夜爱 | 国产女人又爽又大 | 五月激情丁香 | 欧美一区二区手机在线观看视频 | 一区欧美| 欧美人人爽 | 国产一卡2卡3卡四卡精品网 | 欧美在线精品一区二区在线观看 | 日本一道本在线视频 | 色综合天天射 | 天天舔天天射天天干 | 日韩精品欧美激情国产一区 | 在线免费看黄色 | 久久网站免费 | 成人影视免费观看 | 亚洲 欧美 另类中文字幕 | 麻豆入口| 国产麻豆va精品视频 | 好男人社区成人影院在线观看 | 波多野结衣中文字幕在线播放 | 99热免费在线观看 | 亚洲h在线观看 | 日本午夜小视频 | 一级黄色网 | 精品免费tv久久久久久久 | 一级特黄aaa大片免费看 | 欧美一区二区三区香蕉视 | 日韩欧美国产高清在线观看 |