import java.io.*;
import java.util.*;
import corejava.*;public class DataFileTest
{ static void writeData(Employee[] e, PrintWriter out)
throws IOException
{ out.println(e.length);
int i;
for (i = 0; i < e.length; i++)
e[i].writeData(out);
}
static Employee[] readData(BufferedReader in)
throws IOException
{ int n = Integer.parseInt(in.readLine());
Employee[] e = new Employee[n];
int i;
for (i = 0; i < n; i++)
{ e[i] = new Employee();
e[i].readData(in);
}
return e;
}
public static void main(String[] args)
{ Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35500,
new Day(1989,10,1));
staff[1] = new Employee("Carl Cracker", 75000,
new Day(1987,12,15));
staff[2] = new Employee("Tony Tester", 38000,
new Day(1990,3,15));
int i;
for (i = 0; i < staff.length; i++)
staff[i].raiseSalary(5.25);
try
{ PrintWriter out = new PrintWriter(new
FileWriter("employee.dat"));
writeData(staff, out);
out.close();
}
catch(IOException e)
{ System.out.print("Error: " + e);
System.exit(1);
}
try
{ BufferedReader in = new BufferedReader(new
FileReader("employee.dat"));
Employee[] e = readData(in);
for (i = 0; i < e.length; i++) e[i].print();
in.close();
}
catch(IOException e)
{ System.out.print("Error: " + e);
System.exit(1);
}
}
}
class Employee
{ public Employee(String n, double s, Day d)
{ name = n;
salary = s;
hireDay = d;
}
public Employee() {}
public void print()
{ System.out.println(name + " " + salary
+ " " + hireYear());
}
public void raiseSalary(double byPercent)
{ salary *= 1 + byPercent / 100;
}
public int hireYear()
{ return hireDay.getYear();
}
public void writeData(PrintWriter out) throws IOException
{ out.println(name + "|"
+ salary + "|"
+ hireDay.getYear() + "|"
+ hireDay.getMonth() + "|"
+ hireDay.getDay());
} public void readData(BufferedReader in) throws IOException
{ String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, "|");
name = t.nextToken();
salary = Double.parseDouble(t.nextToken());
int y = Integer.parseInt(t.nextToken());
int m = Integer.parseInt(t.nextToken());
int d = Integer.parseInt(t.nextToken());
hireDay = new Day(y, m, d);
} private String name;
private double salary;
private Day hireDay;
}
//??????如何实现????
String line;
while not 结尾
line=file1.readline();
//??????如何实现????
//要使line里面是文件的所有内容
public static void main(String[] args) {
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\temp\\test\\a.txt"));
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
for (int val = in.read(); val != -1; val = in.read()) {
bytes.write(val);
}
BufferedWriter out = new BufferedWriter(new FileWriter("d:\\temp\\test\\a.txt"));
out.write("begin");
out.write(new String(bytes.toByteArray()));
out.write("end");
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}jdk1.4有新的方法可以实现随机访问文件,但是我还没有研究过。
http://www.csdn.net/develop/Read_Article.asp?Id=11165
// process line
}
import java.util.zip.*;
import java.util.*;
public class FileTool
{
public void appendToFile(String str, String filename) throws Exception
{
FileOutputStream stream;//provides file access
OutputStreamWriter writer;//writes to the file
try
{
stream = new FileOutputStream(filename, true);
writer = new OutputStreamWriter(stream);
writer.write(str);
writer.close();
stream.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//appendToFile
public void prependToFile(String str, String filename) throws Exception
{
// Read the file, then prepend str to the file text and write back
// to the file.
String newStr;//final string to be written
try
{
newStr = str + readFile(filename);
writeFile(newStr, filename);
}//try
catch(Exception e)
{
throw e;
}//catch
}//prependToFile
public String readFile(String filename) throws Exception
{
//Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
try
{
breader = new BufferedReader(new FileReader(filename));//header
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readFile
public List readFileToList(String filename) throws Exception
{
//Read the file into a List, then return.
BufferedReader breader;//reader for the template files
List list;//target vector
String line;//line from file
list = new ArrayList();
try
{
breader = new BufferedReader(new FileReader(filename));//header
while((line = breader.readLine()) != null)
list.add(line);
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return list;
}//readFileToVector
public void writeFile(String str, String filename) throws Exception
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile
public void copyFile(String sourcename, String targetname) throws Exception
{
// Open up a reader from sourcename and a writer to targetname.
// Write each character from sourcename to targetname, then close.
BufferedReader breader;//reader from source
BufferedWriter bwriter;//writer to target
try
{
breader = new BufferedReader(new FileReader(sourcename));
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetname)));
while(breader.ready())
bwriter.write(breader.read());
breader.close();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//copyFile
public String readZippedFile(String textFilename, String zipFilename) throws Exception
{
// Open a stream from the entry in the zip file for the file.
// Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
ZipFile zipFile;//zip file
ZipEntry zipEntry;//entry in zip
InputStream iStream;//input stream of file
try
{
zipFile = new ZipFile(zipFilename);
zipEntry = zipFile.getEntry(textFilename);
iStream = zipFile.getInputStream(zipEntry);
breader = new BufferedReader(new InputStreamReader(iStream));
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readZippedFile
public String readConsole() throws Exception
{
// Create a buffered reader with System.in, then
// read a line from it.
BufferedReader breader;
breader = new BufferedReader(new InputStreamReader(System.in));
return breader.readLine();
}//readConsole
public Properties loadProperties(String propsFile) throws Exception
{
// Open up an input stream from the file, then read it into
// a properties hashtable and return.
FileInputStream instream;//the input stream
Properties props;//props object
instream = null;
props = new Properties();
try
{
instream = new FileInputStream(propsFile);
props.load(instream);
}//try
finally
{
try
{
instream.close();
}//try
catch(Exception e1)
{
}
}//finally
return props;
}//loadProperties
//______________________________________________________________
}