Reading and writing files in Java (Input/Output) - Tutorial

來源:互聯網
上載者:User

標籤:

  Java Input Output

This tutorial explains how to read and write files via Java.

 

Table of Contents

1. Java I/O (Input / Output) for files
1.1. Overview
1.2. Reading a file in Java
1.3. Writing a file in Java
1.4. How to identify the current directory
2. Exercise: Reading and writing files
3. Reading resources out of your project / jar
4. About this website
4.1.
5. Links and Literature
1. Java I/O (Input / Output) for files1.1. Overview

Java provides a standard way of reading from and writing to files. Traditionally the java.io package was used, but in modern Java applications you use the java.nio.file API.

Java will read all input as a stream of bytes. The InputStream class is the superclass of all classes representing an input stream of bytes.

1.2. Reading a file in Java

To read a text file you can use the Files.readAllBytes method as demonstrated by the following listing.

 

import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths;// somewhere in your codeString content = new String(Files.readAllBytes(Paths.get(fileName))); 

 

To read a text file line by line into a List of type String structure you can use the following example.

 

List<String> lines = Files.readAllLines(Paths.get(fileName)); 

 

1.3. Writing a file in Java

To write a file you can use the following method:

 

Files.write(Paths.get(fileName), content.getBytes(), StandardOpenOption.CREATE); 

 

1.4. How to identify the current directory

You can access files relative to the current execution directory of your Java program. To print the current directory in which your Java program is running, you can use the following statement.

 

System.out.println(System.getProperty("user.dir")); 

 

 2. Exercise: Reading and writing files

Create a new Java project called com.vogella.java.files. Create the following FilesUtil.java class.

 

package com.vogella.java.files;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths;import java.nio.file.StandardOpenOption;import java.util.List;public class FilesUtil {  public static String readTextFile(String fileName) throws IOException {    String content = new String(Files.readAllBytes(Paths.get(fileName)));    return content;  }    public static List<String> readTextFileByLines(String fileName) throws IOException {    List<String> lines = Files.readAllLines(Paths.get(fileName));    return lines;  }    public static void writeToTextFile(String fileName, String content) throws IOException {    Files.write(Paths.get(fileName), content.getBytes(), StandardOpenOption.CREATE);  }  } 

  

 

To test these methods, create a text file called file.txt with some content in your project folder. Create the following Main class and run it.

 

package com.vogella.java.files;import java.io.IOException;import java.nio.file.Path;import java.nio.file.Paths;public class Main {  public static void main(String[] args) throws IOException {    String input = FilesUtil.readTextFile("file.txt");    System.out.println(input);    FilesUtil.writeToTextFile("copy.txt", input);        System.out.println(FilesUtil.readTextFile("copy.txt"));        FilesUtil.readTextFileByLines("file.txt");    Path path = Paths.get("file.txt");  }}  

  

3. Reading resources out of your project / jar

You can read resources from your project or your jar file via the .getClass().getResourceAsStream() method chain from any object.

Reading and writing files in Java (Input/Output) - Tutorial

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.