งานที่พบบ่อยที่สุดประการหนึ่งในการพัฒนาซอฟต์แวร์คือความสามารถในการอ่านและเขียนไฟล์ ใน Java การเขียนลงไฟล์เป็นกระบวนการที่ไม่ซับซ้อนซึ่งสามารถทำได้โดยใช้คลาสและวิธีการในตัว ในบทความนี้ เราจะสำรวจวิธีการเขียนลงไฟล์โดยใช้ Java และหารือเกี่ยวกับคลาสและวิธีการต่างๆ ที่เกี่ยวข้อง Java มีคลาสในตัว เช่นFileWriterและPrintWriterเพื่อเขียนลงในไฟล์ใน Java
ตัวอย่างการเขียน Java ไปยังไฟล์
ขั้นแรก มาดูตัวอย่างง่ายๆ ของการเขียนไฟล์ใน Java ต่อไปนี้คือตัวอย่างวิธีเขียนสตริงลงในไฟล์ใน Java โดยใช้ คลาส FileWriter :คลาส FileWriter
คลาส FileWriter เป็นอีกวิธี หนึ่งในการเขียนไฟล์ใน Java เป็นคลาสสตรีมอักขระที่ให้คุณเขียนข้อมูลอักขระลงในไฟล์ นี่คือตัวอย่างที่สาธิตวิธีการใช้ คลาส FileWriterเพื่อเขียนสตริงลงในไฟล์:import java.io.FileWriter;
import java.io.IOException;
// Main method for the string to file in java starts from here,
public class WriteToFileExample {
public static void main(String[] args) {
// create a string to write text to file
String data = "Hello, world!";
try {
// create a FileWriter object with the file name
FileWriter writer = new FileWriter("output.txt");
// write the string to the file
writer.write(data);
// close the writer
writer.close();
System.out.println("Successfully wrote text to file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
ในตัวอย่างนี้ ขั้นแรกเราสร้าง ออบเจ็กต์ Stringชื่อdataซึ่งมีข้อความที่เราต้องการเขียนลงในไฟล์ จากนั้นเราสร้าง ออบเจ็ ก ต์ FileWriterด้วยชื่อของไฟล์ที่เราต้องการเขียน ในกรณีนี้คือชื่อoutput.txt ต่อไปเราใช้ เมธอด write()ของ วัตถุ FileWriterเพื่อเขียน สตริง ข้อมูลลงในไฟล์ วิธีการนี้รับ อาร์กิวเมนต์ Stringที่มีข้อมูลที่จะเขียน สุดท้ายนี้ เราจะปิด ออบเจ็กต์ FileWriterเพื่อปล่อยทรัพยากรไฟล์และพิมพ์ข้อความแสดงความสำเร็จไปยังคอนโซลหากการดำเนินการเขียนสำเร็จ หรือข้อความแสดงข้อผิดพลาดหากตรวจพบข้อยกเว้น คลาส FileWriter เป็นวิธี ง่ายๆในการเขียนข้อมูลอักขระไปยังไฟล์ใน Java
คลาส FileOutputStream
คลาส FileOutputStream ใช้ในการเขียนข้อมูลไปยังไฟล์ในรูปแบบสตรีมของไบต์ นี่คือตัวอย่าง:import java.io.FileOutputStream;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
FileOutputStream stream = new FileOutputStream("output.txt");
byte[] bytesToWrite = textToWrite.getBytes();
stream.write(bytesToWrite);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ในตัวอย่างนี้ เราสร้าง ออบเจ็กต์ FileOutputStreamชื่อstreamพร้อมด้วยชื่อไฟล์ "output.txt" สำหรับเขียนถึง จากนั้นเราจะแปลง สตริง textToWriteเป็นอาร์เรย์ไบต์โดยใช้เมธอดgetBytes()และเขียนอาร์เรย์ไบต์ไปยังออบเจ็กต์สตรีมโดยใช้เมธอดwrite () ในที่สุด เราก็ปิดวัตถุ สตรีม
คลาส BufferedWriter
คลาส BufferedWriter ใช้เพื่อเขียนข้อความไปยังไฟล์ที่มีความสามารถในการบัฟเฟอร์ นี่คือตัวอย่าง:import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
writer.write(textToWrite);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ในตัวอย่างนี้ เราสร้าง อ็อบเจ็กต์ BufferedWriterชื่อwriterด้วย อ็อบเจ็กต์ FileWriterที่เขียนลงในไฟล์ "output.txt" จากนั้นเราใช้ เมธอด write()ของ อ็อบเจ็กต์ ตัวเขียนเพื่อเขียนtextToWriteลงในไฟล์ ในที่สุด เราก็ปิดวัตถุ ตัวเขียน
วิธีการ WriteString()
เมธอดwriteString()เป็นวิธีที่สะดวกซึ่งนำมาใช้ใน Java 11 สำหรับการเขียนStringไปยังไฟล์โดยใช้คลาสFiles ช่วยให้คุณสามารถเขียนสตริงลงในไฟล์ด้วยโค้ดบรรทัดเดียว นี่คือตัวอย่าง:import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class WriteToFileExample {
public static void main(String[] args) {
// create a string to write to a file
String data = "Hello, world!";
// create a file object
Path file = Paths.get("output.txt");
try {
// write the string to the file using writeString() method
Files.writeString(file, data);
System.out.println("Successfully wrote text to file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
ในตัวอย่างนี้ ขั้นแรกเราสร้าง ออบเจ็กต์ Stringชื่อdataซึ่งมีข้อความที่เราต้องการเขียนลงในไฟล์ จากนั้นเราสร้าง ออบเจ็กต์ Pathชื่อไฟล์ซึ่งแสดงถึงไฟล์ที่เราต้องการเขียน ในกรณีนี้คือชื่อ "output.txt" ต่อไป เราใช้ เมธอด Files.writeString()เพื่อเขียน สตริง ข้อมูลไปยังไฟล์ที่แสดงโดยอ็อบเจ็กต์ไฟล์ วิธีการนี้รับสองอาร์กิวเมนต์: อ็อบเจ็กต์ Pathที่แสดงไฟล์ที่จะเขียน และ อ็อบเจ็กต์ Stringที่มีข้อมูลที่จะเขียน สุดท้ายนี้ เราจะพิมพ์ข้อความแสดงความสำเร็จไปยังคอนโซลหากการดำเนินการเขียนสำเร็จ หรือข้อความแสดงข้อผิดพลาดหากตรวจพบข้อยกเว้น เมธอดwriteString()เป็นวิธีที่สะดวกในการเขียนสตริงไปยังไฟล์โดยใช้ Java 11 หรือใหม่กว่า
คลาสนักเขียนภาพพิมพ์
คลาสPrintWriterจะล้างบัฟเฟอร์เอาต์พุตโดยอัตโนมัติหลังจากเขียนแต่ละบรรทัด เพื่อให้มั่นใจว่าข้อมูลจะถูกเขียนลงในไฟล์ทันที ทำให้เป็นตัวเลือกที่สะดวกสำหรับการเขียนข้อมูลข้อความจำนวนมากลงในไฟล์ นี่คือตัวอย่าง:import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
PrintWriter writer = new PrintWriter(new FileWriter("output.txt"));
writer.println(textToWrite);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ในตัวอย่างนี้ เราสร้าง อ็อบเจ็กต์ PrintWriterชื่อwriterด้วย อ็อบเจ็กต์ FileWriteที่เขียนลงในไฟล์ "output.txt" จากนั้นเราใช้ เมธอด println()ของ อ็อบเจ็กต์ตัว เขียนเพื่อเขียนtextToWriteลงในไฟล์ ในที่สุด เราก็ปิดวัตถุ ตัวเขียน
คลาส DataOutputStream
คลาส DataOutputStream ใช้เพื่อเขียนชนิดข้อมูล Java ดั้งเดิมลงในไฟล์ในรูปแบบสตรีมของไบต์ นี่คือตัวอย่าง:import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
int numberToWrite = 42;
try {
DataOutputStream stream = new DataOutputStream(new FileOutputStream("output.txt"));
stream.writeUTF(textToWrite);
stream.writeInt(numberToWrite);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ในตัวอย่างนี้ เราสร้าง ออบเจ็กต์ DataOutputStreamชื่อstreamซึ่งเขียนลงในไฟล์ "output.txt" จากนั้นเราใช้ เมธอด writeUTF()ของสตรีมอ็อบเจ็กต์เพื่อเขียน สตริง textToWriteและ เมธอด writeInt()เพื่อเขียน จำนวนเต็ม numberToWriteลงในไฟล์ ในที่สุด เราก็ปิดวัตถุ สตรีม
คลาส FileChannel
คลาส FileChannel จัดเตรียมวิธีการเขียนข้อมูลไปยังไฟล์โดยใช้ I/O ที่แมปหน่วยความจำ นี่คือตัวอย่าง:import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
File file = new File("output.txt");
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
FileChannel channel = randomAccessFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(textToWrite.getBytes());
buffer.flip();
channel.write(buffer);
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ในตัวอย่างนี้ ขั้นแรกเราสร้าง ออบเจ็กต์ ไฟล์ชื่อไฟล์โดยใช้ชื่อไฟล์ "output.txt" เป็นพารามิเตอร์ จากนั้นเราสร้าง วัตถุ RandomAccessFileชื่อRandomAccessFileโดยมี วัตถุ ไฟล์และโหมด "rw" เป็นพารามิเตอร์ จากนั้นเราสร้าง วัตถุ FileChannelชื่อchannelซึ่งเริ่มต้นได้โดยใช้วัตถุRandomAccessFile จากนั้นเราสร้าง วัตถุ ByteBufferชื่อบัฟเฟอร์ที่มีความจุ 1,024 ไบต์และใส่ สตริง textToWriteเป็นพารามิเตอร์ จากนั้นเราพลิก วัตถุ บัฟเฟอร์เพื่อเตรียมสำหรับการเขียนและเขียนลงในวัตถุช่องโดยใช้วิธีwrite() ในที่สุดเราก็ปิดวัตถุ RandomAccessFile
GO TO FULL VERSION