wps打印表头每页都有:Java高手请进!!有关于文件编程题请教

来源:百度文库 编辑:高考问答 时间:2024/04/29 01:54:37
Suppose you have one file myfile.txt in “c:\temp” folder, please write one program to do followings:
A.Print out the following attributes of the file:
Name, parent directory name, last modified date and size
B.Replace word “development” in the file “myfile.txt” with “Java coding” and save the new file with name “newfile.txt”.
用Java语言写!!!

import java.io.*;
import java.util.Date;
import java.text.DateFormat;

public class FileTest {

public static void main(String[] args) throws Exception {

// A.Print out the following attributes of the file:
// Name, parent directory name, last modified date and size
File file = new File("c:\\temp", "myfile.txt");
System.out.println("Name: " + file.getName());
System.out.println("Parent: " + file.getParent());
Date date = new Date(file.lastModified());
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
System.out.println("Last modified date: " + dateFormat.format(date));
System.out.println("Size: " + file.length());

// B.Replace word "development" in the file "myfile.txt" with
// "Java Coding" and save the new file with name "newfile.txt".
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);

File newFile = new File(file.getParent(), "newfile.txt");
FileOutputStream fos = new FileOutputStream(newFile);
FileWriter fw = new FileWriter(newFile, true);
BufferedWriter bw = new BufferedWriter(fw);
String aLine = br.readLine();
while(aLine != null) {
aLine = aLine.replaceAll("development", "Java coding");
bw.write(aLine);
bw.newLine();
aLine = br.readLine();
}
br.close();
bw.close();

}
}