Thứ Năm, 27 tháng 11, 2008

Thực hành Stream and File

/*
* Created on 2 janv. 2009
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day30_2009jan1;

/**
* @author Administrator
*
* TODO Tao ra khung Frame hien thi cac thu muc va tap tin trong o dia cung.
* page 156/205 JavaTV
*/
import java.awt.*;//Bo giao dien trinh bay.
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
public class FileDemo {

public static void main(String[] args) {
Frame fr = new Frame("File Demo");
fr.setBounds(10, 10, 300, 200);
fr.setLayout(new BorderLayout());
Panel p = new Panel(new GridLayout(1, 2));
List list_C = new List();
list_C.add("F:\\");
//tao doi tuong file.
File driver_C = new File("F:\\");
String[] dirs_C = driver_C.list();
for(int i=0; i <>
File f = new File("F:\\" + dirs_C[i]);
//kiem tra xem tap tin co phai la thu muc khong?
if(f.isDirectory()){
list_C.add("
" + dirs_C[i]);
}else{
list_C.add("Day la file: " + dirs_C[i]);
}
}
List list_D = new List();
list_D.add("E:\\");
File driver_D = new File("E:\\");
String[] dirs_D = driver_D.list();
for(int i=0; i <>
File f = new File("E:\\" + dirs_D[i]);
if(f.isDirectory()){
list_D.add("" + dirs_D[i]);
}else{
list_D.add("Day la file:" + dirs_D[i]);
}
}
p.add(list_C);
p.add(list_D);
fr.add(p, BorderLayout.CENTER);
fr.setVisible(true);
fr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}//end class.

/*
* Created on 29 déc. 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day27_2008dec28;

/**
* @author Administrator
*
* TODO Dung DataOutputStream va DataInputStream de ghi va doc
* nhung kieu du lieu khac nhau tren file.
* page 142/205
*/
import java.io.*;
class RWData {

public static void main(String[] args) throws IOException{
//hien thuc interface DataOutput
DataOutputStream dataOut;
DataInputStream dataIn;
int i = 10;
double d = 1023.56;//kieu so thuc
boolean b = true;
try{
//DataOutputStream : Luong xuat co nhung phuong thuc ghi
//nhung kieu du lieu chuan trong java.
//fileOutputStream : Luong xuat cho phep ghi du lieu xuong file.
dataOut = new DataOutputStream(new FileOutputStream("F:\\testdata.txt"));
}catch(IOException exc){
System.out.println("Cannot open file.");
return;
}
try{
System.out.println("Writing " + i);
dataOut.writeInt(i);
System.out.println("writing " + d);
dataOut.writeDouble(d);
System.out.println("Writing " + b);
dataOut.writeBoolean(b);
System.out.println("Writing " + 12.2*7.4);
dataOut.writeDouble(12.2*7.4);
}catch(IOException exc){
System.out.println("Write error.");
}
dataOut.close();
System.out.println();
//Now, read them back.
try{
//DataInputStream : Luong nhap co nhung phuong thuc doc
//nhung kieu du lieu chuan trong java.
//FileInputStream : Luong nhap cho phep doc du lieu tu file.
dataIn = new DataInputStream(new FileInputStream("F:\\testdata"));
}catch(IOException exc){
System.out.println("Cannot open file.");
return;
}
try{
i = dataIn.readInt();
System.out.println("Reading " + i);
d = dataIn.readDouble();
System.out.println("Reading " + d);
b = dataIn.readBoolean();
System.out.println("Reading " + b);
d = dataIn.readDouble();
System.out.println("Reading " + d);
}catch(IOException exc){
System.out.println("Read error");
}
dataIn.close();
}
}//end class.

/*
* Created on Dec 5, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day26_2008dec4;

/**
* @author Administrator
*
* TODO Copy noi dung mot file text den mot file text khac
* page 139/205
*/
import java.io.*;
class CopyFile {

public static void main(String[] args) throws IOException{
int i;
FileInputStream fin;
FileOutputStream fout;
try{
//open input file
try{
fin = new FileInputStream("F:\\source.txt");
}catch(FileNotFoundException exc){
System.out.println("Input File Not Fould");
return;
}
//open output file
try{
fout = new FileOutputStream("F:\\dest.txt");
}catch(FileNotFoundException exc){
System.out.println("Error Opening Output File");
return;
}
}catch(ArrayIndexOutOfBoundsException exc){
System.out.println("Usage : CopyFile from To");
return;
}
//copy file
try{
do{
i = fin.read();
if(i != -1)fout.write(i);
//tra ve -1 khi het file
}while(i != -1);
//nem ra IOException khi co loi doc
}catch(IOException exc){
System.out.println("File Error");
}
fin.close();
fout.close();
}
}//end class

- /*
* Created on Dec 1, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day25_2008nov30;

/**
* @author Administrator
*
* TODO Hien thi noi dung cua mot file ten text.txt luu tai
* D:\text.txt
* page 137
*/
import java.io.*;
class ShowFile {

public static void main(String[] args) throws IOException{
int i;
//Mo mot file de doc du lieu.
FileInputStream fin;
try{
fin = new FileInputStream("D:\\text.txt");
//neu file khong ton tai thi nem ra FileNotFoundException.
}catch(FileNotFoundException exc){
System.out.println("File Not Found");
return;
}
catch(ArrayIndexOutOfBoundsException exc){
System.out.println("Usage : ShowFile File");
return;
}
//Read bytes until EOF is encountered
do{
i = fin.read();
if(i != -1) System.out.print((char) i);
}while(i != -1);
fin.close();
}
}//end class

/*
* Created on Nov 24, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day23_2008nov23;

/**
* @author Administrator
*
* TODO Chuong trinh minh hoa viec doc mot mang bytes tu System.in
* page 135
*/
import java.io.*;
class ReadBytes {
public static void main(String[] args) throws IOException{
//khai bao mang data[] kieu byte voi kich thuoc 100.
byte data[] = new byte[100];
System.out.print("Enter some characters : ");
//Luong nhap chuan, mac dinh la ban phim, la mot doi tuong
//kieu InputStream.
System.in.read(data);
System.out.print("you entered : ");
for(int i=0; i <>
System.out.print((char)data[i]);
}
}

Không có nhận xét nào: