程序一:代码 对temp文件夹下的子文件夹进行读取以及计算数量
/**
* @(#)FileInoutput.java
*
* FileInoutput application
*
* @author
* @version 1.00 2007/10/16
*/
import java.io.*;
public class FileInoutput {
public static void main(String[] args) {
// TODO, add your application code
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String sdir="C://temp";
String sfile;
File Fdir1=new File(sdir);
if( Fdir1.exists() && Fdir1.isDirectory())
{
System.out.println("There is a directory: "+sdir+" exits ");
for(int i=0;i<Fdir1.list().length;i++)
System.out.println((Fdir1.list())[i]);
File Fdir2=new File("c://temp//temp");
if(!Fdir2.exists())
Fdir2.mkdir();
System.out.println();
System.out.println("now the new list after create a new dir: ");
for (int i = 0; i<Fdir1.list().length; i++)
System.out.println((Fdir1.list())[i]);
}
System.out.println("Enter a file name in this directory: ");
sfile=in.readLine();
File Ffile=new File(Fdir1,sfile);
if(Ffile.isFile())
{
System.out.println("File "+Ffile.getName()
+"in Path "+Ffile.getPath()
+"is "+Ffile.length()+"in length.");
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
代码二: 对temp文件夹下的crx,txt文件进行读取以及书写
/**
* @(#)MyFileIo.java
*
*
* @author
* @version 1.00 2007/10/16
*/
import java.io.*;
public class MyFileIo {
public static void main(String []args){
char ch;
int chi;
File MyPath=new File("c://temp");
if(!MyPath.exists())
{ MyPath.mkdir();
}
File MyFile1=new File(MyPath,"crt.txt");
try {
FileOutputStream fout=new FileOutputStream(MyFile1);
System.out.println("inout a String finished with # please: ");
while((ch=(char)System.in.read())!='#')
fout.write(ch);
fout.close(); //close the file
System.out.println(""); // /n
FileInputStream fin=new FileInputStream(MyFile1);
while((chi=fin.read())!=-1)
System.out.print((char)chi);
fin.close();
}
catch (FileNotFoundException ex) {
System.err.println(ex);
}
catch (IOException ex) {
System.err.println(ex);
}
}//main()
}//class MyFileIo