Trên Windows, đường dẫn sẽ là: \a\b. Trên Unix, đường dẫn có thể là /a/b
// Tạo đối tượng File File file =new File(“filename”); //// Chuyển đốI tượng file sang URL URL url =null; try{ url = file.toURL(); }catch(MalformedURLException me){} // Chuyển đổi từ URL sang File file =new File(url.getFile());
Lấy thư mục làm việc hiện hành:
String curDir =System.getProperty(“user.dir”);
Copy từ file này sang file khác:
void copy(File src,File dest)throwsIOException{ InputStream is=new InputStream(src); OutputStream os =new OutputStream(dest); byte[] buf =new byte[1024]; int len; while((len =is.read(buf))>0){ os.write(buf,0, len); } is.close(); os.close(); }
Đọc dữ liệu text từ luồng nhập chuẩn:
try{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String str =null; while((str =in.readLine())!=null){ // xử lý dữ liệu } in.close(); }catch(IOException ioe){}
Đọc dữ liệu từ tập tin văn bản:
try{ BufferedReader in=new BufferedReader(new FileReader(“tentaptin”)); String str =null; while((str =in.readLine())!=null) {// xử lý dữ liệu }}catch(IOException ioe){}
Ghi dữ liệu text ra tập tin:
try{ BufferedWriter out=new BufferedWriter(new FileWriter(“tentaptin”)); out.write(“ChaoJavaVietNam”); out.close(); }catch(IOException ioe){}
Ghi thêm dữ liệu vào tập tin:
try{ BufferedWriter out=new BufferedWriter(new FileWriter(“tentaptin”,true)); out.write(“ChaoJavaVietNam”); out.close(); }catch(IOException ioe){}
Sử dụng Random Access File:
try{ File file =new File(“tentaptin”); RandomAccessFile rf =new RandomAccessFile(file,“rw”); // Đọc một kí tự char c = rf.readChar();// Di chuyển đến cuối tập tin rf.seek(file.length()); // Thêm dữ liệu vào cuối tập tin rf.writeChars(“ChaoJavaVietNam”); rf.close(); }catch(IOException ioe){}
Kiểm tra thư mục hay tập tin có tồn tại hay không ?
File file =new File(“tentaptin”); // new File(“tenthumuc”); boolean isExists = file.exists(); if(isExists){ // làm gì đó }else{ // cũng làm gì đó}
Đọc dữ liệu từ tập tin vào mảng byte:
public static byte[] getBytesFromFile(File file)throwsIOException{ InputStream is=new InputStream(file);// Lấy chiều dài của tập tin long length = file.length(); if(length >Integer.MAX_VALUE){ // tập tin quá lớn } //Tạo mảng byte để chứa dữ liệu byte[] bytes =new byte[(int)length]; // Đọc dữ liệu vào bytes int offset =0; int numRead =0; while(offset < bytes.length
&&(numRead=is.read(bytes, offset, bytes.length-offset)) >=0){ offset += numRead; } // Kiểm tra xem đã đọc hết toàn bộ tập tin hay chưa if(offset < bytes.length){ thrownewIOException("Không thể đọc hết tập tin "+file.getName()); } // Đóng luồng is.close(); return bytes; }
Ghi một đối tượng ra tập tin:
Object object=new javax.swing.JButton(“JavaVietNam”); try{ ObjectOutputout=new ObjectOutputStream(newFileOutputStream(“tentaptin”)); out.writeObject(object); out.close(); }catch(IOException ioe){}
Lấy một đốI tượng đã được ghi ra tập tin:
try{ File file =new File(“tentaptin”); ObjectInputStreamin=new ObjectInputStream(newFileInputStream(file)); javax.swing.JButton b =(javax.swing.JButton)in.readObject(); // Sử dụng đối tượng JButton bình thường }catch(IOException ioe){}
Duyệt qua tất cả các tập tin và thư mục trong một thư mục:
public static void visitAllDirsAndFiles(File dir){ // làm gì đó với dir ở đây…//……………………… if(dir.isDirectory()){ String[] children = dir.list(); for(int i=0; i
{visitAllDirsAndFiles(newFile(dir, children)); } } }
Thông tin
Hiện có 1 đang Online, 1 visit trong ngày,1 visit trong tháng. Tổng lượt truy cập là 464 lượt.