程式設計師在使用需要讀寫檔案的 Java 應用程式時使用 Java 中的文字檔案。 文字檔案是儲存資訊、程式碼或任何其他資料的通用方式。文字檔案被視為水平組織的字元序列。 Java 中的文字檔案具有副檔名,例如包含 Java 程式碼的 .java。 Java 提供了不同的實用程序,可讓您透過讀取或寫入純文字檔案來處理它們。您可以根據您的理解選擇任何讀取/寫入實用程式。
在開始使用這些方法之前,我們正在考慮路徑“/Users/praashibansal/Desktop/Data/test.txt”處的文字檔案“test.txt”,其內容為“Hello,there”。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗代碼:
import java.io.*; public class BReader { public static void main(String[] args) throws Exception { File f = new File( "https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); // Creating an object BufferedReader b = new BufferedReader(new FileReader(f)); // Declaring a string variable String s; // Condition holds till // there is a character in a string while ((s = b.readLine()) != null) // Print the string System.out.println(s); } }
輸出:
代碼:
import java.io.*; public class RFile { public static void main(String[] args) throws Exception { // Passing the file’s path FileReader f = new FileReader( "https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); // declaring loop variable int i; while ((i = f.read()) != -1) // Print the content of a file System.out.print((char)i); } }
輸出:
代碼:
import java.io.File; import java.util.Scanner; public class ReadScan { public static void main(String[] args) throws Exception { // passing the file’s path File file = new File("https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); Scanner s = new Scanner(file); while (s.hasNextLine()) System.out.println(s.nextLine()); } }
輸出:
代碼:
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FCExample { public static void main(String[] args) { Path path = Paths.get("https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); try { byte[] b = Files.readAllBytes(path); System.out.println("Read bytes: \n"+new String(b)); } catch (IOException e) { } } }
輸出:
Code:
import java.io.FileWriter; public class FWFile { public static void main(String args[]){ try{ FileWriter f=new FileWriter("https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); f.write("Hello"); f.close(); } catch(Exception e) { System.out.println(e); } System.out.println("Hello"); } }
Output:
Code:
import java.io.FileWriter; import java.io.BufferedWriter; import java.io.IOException; public class BRExample { public static void main(String args[]) { String data = "data for output file"; try { // Creates a FileWriter FileWriter file = new FileWriter("https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); try ( // Creates a BufferedWriter var o = new BufferedWriter(file)) { // Writes the string to the file o.write(data); } } catch (IOException e) { e.getStackTrace(); } } }
Output:
Code:
import java.io.FileOutputStream; import java.io.IOException; public class GFG { public static void main(String[] args) { String f = "Hello"; FileOutputStream o = null; // starting Try block try { // creating an object of FileOutputStream o = new FileOutputStream("https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); // storing byte content from string byte[] str = f.getBytes(); // writing into the file o.write(str); // printing success message System.out.print( "data added successfully."); } // Catch block for exception handling catch (IOException e) { System.out.print(e.getMessage()); } finally { // closing the object if (o != null) { // checking if the file is closed try { o.close(); } catch (IOException e) { // showing exception message System.out.print(e.getMessage()); } } } } }
Output:
Code:
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; /** * Java Files write file example * * @author pankaj * */ public class FCExample { public static void main(String[] args) { Path path = Paths.get("https://cdn.educba.com/Users/praashibansal/Desktop/Data/test.txt"); try { String str = "Example"; byte[] bs = str.getBytes(); Path w = Files.write(path, bs); System.out.println("Written content in file:\n"+ new String(Files.readAllBytes(w))); } catch (IOException e) { } } }
Output:
Reading and writing a file in Java is a straightforward process. With the availability of different methods and utilities in Java, you can choose a specific way to read from and write to a file. Each utility has its functionality that makes it different from others.
Answer: To read, you can use Reader Class or utility class. Some utility classes are- File Class, FileReader, BufferedReader, and Scanner class.
Answer: To write a file in Java, you can use FileWriter, BufferedWriter, java 7 Files, FileOutputStream, and many other methods.
Answer: You can easily import the File class from the java.io package to work with files.
以上是Java 中的文字文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!