Java高手请进!!有关于文件编程题请教

来源:百度知道 编辑:UC知道 时间:2024/05/21 23:27:14
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&q