一阶段-SixteenthWork-读写文件

来源:互联网 发布:淘宝首页模板怎么制作 编辑:程序博客网 时间:2024/06/05 05:10
/*
在F盘下建立一个名为“学生成绩单”的目录,在里面新建一个名为“JAVA成绩.txt”的txt文件在里面填写以下信息:
  张三      90
  李四      95
  王五      88
  赵六      75
String str ="张三\t90\r\n李四\t95\r\n王五\t88\r\n赵六\t75\r\n";
1.编写程序查看此文件或目录是否存在,若存在,判断是目录还是文件,
得到它的名字和绝对路径,以及它的大小。
2.若此文件存在,编写程序查看此文件的内容(使用FileInputStream)。
3.若此文件存在,向此文件添加一名学生的成绩(孙七  100)(使用FileOutputStream)。
4. 复制该文件 内容
java.txt    new.txt
*/
package SixteenthWork;

public class FileUsed
{
    public static void main(String[] args)
    {
        /*
         * 下面是题目的各个实现,测试时应该屏蔽掉其它题号部分再运行程序
        */
        //1、新建一个文件
        FileNew newFile = new FileNew();//用于生成一个新的文件
        newFile.inintFile();
        //2、编写程序查看此文件或目录是否存在
        FileJudge judge = new FileJudge();//用于判断文件的属性
        judge.judge();
        //3、编写程序查看此文件的内容
        FileLookOut fileOut = new FileLookOut();//用于查询文件内容
        fileOut.lookOut();
        //4、向此文件添加一名学生的成绩(孙七  100)
        UpdateFile uf = new UpdateFile();//用于更新文件信息
        uf.updateData();
        //5、复制该文件 内容
        CopyFile cf = new CopyFile();//用于复制文件信息到一个新的文件
        cf.copy();
    }
}
=====================================

package SixteenthWork;
import java.io.*;

public class FileNew
{
    public String inintFile()
    {
        String fileName = "";
        System.out.println("请输入您要新建的文件名和路径:");
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader buff = new BufferedReader(in);
        try
        {
            fileName = buff.readLine();//获取键盘输入的文件路径名
        }catch(Exception e){
            System.out.println(e.toString());
        }
        File file = new  File(fileName);//路径名与文件类绑定
        String str ="张三\t90\r\n李四\t95\r\n王五\t88\r\n赵六\t75\r\n";
        FileWriter fw;
        try
        {
            if(!file.canExecute())
            {
                 file.createNewFile();//新建文件
                 fw = new FileWriter(file);//实例一个FileWriter对象
                 fw.append(str,0,str.length());//向文件中写入字符串
                 fw.flush();//刷新该文件
                 if(file.canExecute())//如果文件生成成功
                 {
                   System.out.println("文件生成成功!");
                 }
            }
            else//如果文件生成失败
            {
                System.out.println("该文件已经存在,无需重新创建!");
            }
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }
        return fileName;//返回文件的路径名
    }

}

========================================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package SixteenthWork;
import java.io.*;
public class FileJudge  //查看此文件或目录是否存在,若存在,判断是目录还是文件
{
    FileNew fileNew = new FileNew();
    public void judge()
    {
        File file = new File(fileNew.inintFile());//获取文件路径
        if(file.canExecute())//判断获取文件是否成功
        {
            if(file.isDirectory())//判断这是一个目录
            {
                 System.out.println("属性: 目录");
                 System.out.println("目录: "+file.getName());
                 System.out.println("目录绝对路径: "+file.getAbsolutePath());
                 System.out.println("目录大小: "+file.length()+" 字节");
            }
            if(file.isFile())//判断这是一个文件
            {
                 System.out.println("属性: 文件");
                 System.out.println("文件名: "+file.getName());
                 System.out.println("文件绝对路径: "+file.getAbsolutePath());
                 System.out.println("文件大小: "+file.length()+" 字节");
            }
        }
        else{
             System.out.println("无法获得对应的文件");
        }
    }
}
=============================================

package SixteenthWork;
import java.io.*;
public class FileLookOut
{
    public void lookOut()
    {
        FileNew fileNew = new FileNew();//获取新建的文件实例
//        Reader re = null;           //以字符读取
        try{
            //把获得的文件名字传进FileInputStream中,从文件读入字节,以byte形式保存
             FileInputStream fileIn = new FileInputStream(fileNew.inintFile() );
             InputStreamReader in = new InputStreamReader(fileIn); //从文件到系统
//             re = in;//把字节转成字符
             int num = 0;
             while((num = in.read()) != -1)
             {
                 System.out.print((char)num);
             }
             in.close();
//             int num = 0;
//             while((num = re.read()) != -1)//以每次读取单个字符的方式读取,并保存到整数num中
//             {
//                 System.out.print((char)num);//把保存了的单个整数打印出来
//             }
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }
//        try
//        {
//            re.close();//关闭该流并释放与之关联的所有资源。
//        }catch(Exception e)
//        {
//            System.out.println(e.toString());
//        }
    }
}
======================================

package SixteenthWork;
import java.io.*;

public class UpdateFile
{
    public void updateData()
    {
        FileNew fileNew = new FileNew();//获取已经生成的文件
        Writer wr = null; 
        try
        {
            FileOutputStream fileOut = new FileOutputStream(fileNew.inintFile(),true);//字节输入流,true设置为在文件内容之后写进新内容
            OutputStreamWriter ow = new OutputStreamWriter(fileOut);//字符输入流
            wr = ow;//子类Writer,这里可以不用,而直接用ow,是一样的效果
            String str = "孙七\t100\r\n";          
            wr.write(str);  //向文件写进新的字符串
            wr.flush();
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }
        try{
            wr.close();
        }catch(Exception e1)
        {
            System.out.println(e1.toString());
        }
    }
}
=====================================

package SixteenthWork;
import java.io.*;


public class CopyFile
{
    public void copy()
    {       
        try {
            FileNew file = new FileNew();
            String fs = new String(file.inintFile());
            //把文件信息读取出来
            FileInputStream fis = new FileInputStream(fs);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader buff1 = new BufferedReader(isr);
            char[] c = new char[100];
            int num = 0;
//            while(isr.read() != -1)
//            {
////                 for(int i=0;i<c.length;i++)
////                {
////                    num = isr.read();
////                        System.out.print((char)num);
////                    c[i] =(char) num;
////                }
//            }
            isr.read(c, 0, c.length);
            String str = new String(c);
           System.out.println(str);

            //把文件信息写进新文件
            System.out.println("请输入你想复制"+fs+"到达的目标路径:");
            InputStreamReader in = new InputStreamReader(System.in);
            BufferedReader buff2 = new BufferedReader(in);
            String newStr = buff2.readLine();
            File fi = new File(newStr);
            fi.createNewFile();
            FileOutputStream fos = new FileOutputStream(fi);
            OutputStreamWriter osr = new OutputStreamWriter(fos);
            osr.write(c,0,c.length);
            osr.flush();
//            fis.close();
//            osr.close();
        }catch(IOException e)
        {
            System.out.println(e.toString());
        }
    }

}


原创粉丝点击