1)
Mkdir()
方法:在E盘根下建立文件夹java和在java下建立子文件夹zy;
2)
write()
方法:在文件夹zy下建立文件yy.txt,并将从键盘随机输入的若干数据写入到文件yy.txt中。
3)
read()
方法:从文件yy.txt中读取内容并输出。
4)
方法copyfile():将文件yy.txt拷贝到java下并生成文件f1.txt。
5)
pp()
方法:求取并输出文件yy.txt的路径、长度以及它的父目录。
import
java.io.
*
;
public class shiyan4
... {
private String dirmain="e:/java";
private String dirsub="E:/java/zy";
public static void main(String[] args)throws IOException
...{
String dirmain="e:/java";
String dirsub="E:/java/zy";
shiyan4 filep=new shiyan4();
filep.MKdir(dirmain);
filep.MKdir(dirsub);
System.out.println("目录创建成功。");
filep.write();
System.out.println("向文件写数据“中华人民共和国”成功。");
System.out.println("读出的文件数据是:");
System.out.println(filep.read("E:/java/zy/yy.txt"));
filep.copyfile();
System.out.println("复制文件成功。");
filep.pp("E:/java/zy/yy.txt");
}
public void pp(String f)
...{
File file=new File(f);
System.out.println("文件绝对路径:");
System.out.println(file.getPath());
System.out.println("文件长度:");
System.out.println(file.length());
System.out.println("文件父目录:");
System.out.println(file.getParent());
}
public void copyfile()throws IOException
...{
this.copyFile("E:/java/zy/yy.txt", "E:/java/f1.txt");
this.renameFile("E:/java", "yy.txt", "f1.txt");
}
public void write()throws IOException
...{
this.createFile(dirsub, "yy.txt");
try ...{
FileOutputStream out=new FileOutputStream("E:/java/zy/yy.txt");
PrintStream p=new PrintStream(out);
System.out.println("输入文件的数据是:");
p.println("中华人民共和国。");
}
catch (FileNotFoundException e)...{
e.printStackTrace();
}
}
public void MKdir(String path)
...{
File dir=new File(path);
if(!dir.exists())
dir.mkdir();
}
public void createFile(String path,String filename) throws IOException
...{
File file=new File(path+"/"+filename);
if(!file.exists())
file.createNewFile();
}
public String read(String path) throws IOException
...{
File file=new File(path);
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
temp=br.readLine();
while(temp!=null)...{
sb.append(temp+" ");
temp=br.readLine();
}
return sb.toString();
}
public void copyFile(String src,String dest) throws IOException
...{
FileInputStream in=new FileInputStream(src);
File file=new File(dest);
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
int c;
byte buffer[]=new byte[1024];
while((c=in.read(buffer))!=-1)
...{
for(int i=0;i<c;i++)
out.write(buffer[i]);
}
in.close();
out.close();
}
public void renameFile(String path,String oldname,String newname)
...{
if(!oldname.equals(newname))...{
File oldfile=new File(path+"/"+oldname);
File newfile=new File(path+"/"+newname);
if(newfile.exists())
System.out.println(newname+"重命名完成!");
else
...{
oldfile.renameTo(newfile);
}
}
}
}
public class shiyan4
... {
private String dirmain="e:/java";
private String dirsub="E:/java/zy";
public static void main(String[] args)throws IOException
...{
String dirmain="e:/java";
String dirsub="E:/java/zy";
shiyan4 filep=new shiyan4();
filep.MKdir(dirmain);
filep.MKdir(dirsub);
System.out.println("目录创建成功。");
filep.write();
System.out.println("向文件写数据“中华人民共和国”成功。");
System.out.println("读出的文件数据是:");
System.out.println(filep.read("E:/java/zy/yy.txt"));
filep.copyfile();
System.out.println("复制文件成功。");
filep.pp("E:/java/zy/yy.txt");
}
public void pp(String f)
...{
File file=new File(f);
System.out.println("文件绝对路径:");
System.out.println(file.getPath());
System.out.println("文件长度:");
System.out.println(file.length());
System.out.println("文件父目录:");
System.out.println(file.getParent());
}
public void copyfile()throws IOException
...{
this.copyFile("E:/java/zy/yy.txt", "E:/java/f1.txt");
this.renameFile("E:/java", "yy.txt", "f1.txt");
}
public void write()throws IOException
...{
this.createFile(dirsub, "yy.txt");
try ...{
FileOutputStream out=new FileOutputStream("E:/java/zy/yy.txt");
PrintStream p=new PrintStream(out);
System.out.println("输入文件的数据是:");
p.println("中华人民共和国。");
}
catch (FileNotFoundException e)...{
e.printStackTrace();
}
}
public void MKdir(String path)
...{
File dir=new File(path);
if(!dir.exists())
dir.mkdir();
}
public void createFile(String path,String filename) throws IOException
...{
File file=new File(path+"/"+filename);
if(!file.exists())
file.createNewFile();
}
public String read(String path) throws IOException
...{
File file=new File(path);
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
temp=br.readLine();
while(temp!=null)...{
sb.append(temp+" ");
temp=br.readLine();
}
return sb.toString();
}
public void copyFile(String src,String dest) throws IOException
...{
FileInputStream in=new FileInputStream(src);
File file=new File(dest);
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
int c;
byte buffer[]=new byte[1024];
while((c=in.read(buffer))!=-1)
...{
for(int i=0;i<c;i++)
out.write(buffer[i]);
}
in.close();
out.close();
}
public void renameFile(String path,String oldname,String newname)
...{
if(!oldname.equals(newname))...{
File oldfile=new File(path+"/"+oldname);
File newfile=new File(path+"/"+newname);
if(newfile.exists())
System.out.println(newname+"重命名完成!");
else
...{
oldfile.renameTo(newfile);
}
}
}
}