1. Trang chủ
  2. » Thể loại khác

java filereader class

2 113 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Lớp FileReader trong Java

    • Ví dụ

Nội dung

http://vietjack.com/java/index.jsp Copyright © vietjack.com Lớp FileReader Java Lớp FileReader Java kế thừa từ lớp InputStreamReader FileReader sử dụng để đọc luồng ký tự Lớp có số constructor để tạo đối tượng cần thiết Cú pháp sau tạo FileReader Java, cung cấp File để đọc từ FileReader(File file) Cú pháp sau tạo FileReader Java, cung cấp FileDescriptor để đọc từ FileReader(FileDescriptor fd) Cú pháp sau tạo FileReader Java, cung cấp tên file để đọc từ FileReader(String fileName) Khi bạn có đối tượng FileReader, có số phương thức sau giúp bạn thao tác file STT Phương thức Miêu tả public int read() throws IOException Đọc ký tự đơn Trả int, mà biểu diễn ký tự đọc public int read(char [] c, int offset, int len) Đọc ký tự bên mảng Trả số ký tự đọc Ví dụ Sau ví dụ minh họa lớp FileReader Java: import java.io.*; public class FileRead{ http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com public static void main(String args[])throws IOException{ File file = new File("Hello1.txt"); // creates the file file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); // Writes the content to the file writer.write("This\n is\n an\n example\n"); writer.flush(); writer.close(); //Creates a FileReader Object FileReader fr = new FileReader(file); char [] a = new char[50]; fr.read(a); // reads the content to the array for(char c : a) System.out.print(c); //prints the characters one by one fr.close(); } } Nó cho kết sau: This is an example http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... writer.write("This is an example "); writer.flush(); writer.close(); //Creates a FileReader Object FileReader fr = new FileReader( file); char [] a = new char[50]; fr.read(a); // reads the content...http://vietjack.com /java/ index.jsp Copyright © vietjack.com public static void main(String args[])throws IOException{

Ngày đăng: 03/12/2017, 00:45