文件获取

来源:互联网 发布:学生赚钱软件 编辑:程序博客网 时间:2024/05/22 11:31
package ck;//创建包ckimport java.io.*;import java.util.*;//调用系统的类public class ew //定义类用于处理文件的操作{    static final String input="d:/student.txt";//定义输入文件路径    static final String output="d:/student.txt";//定义输出文件路径public static void main(String[] args) throws FileNotFoundException{                // TODO 自动生成的方法存根                int i;                String st;                RandomAccessFile r=new RandomAccessFile(input,"rw");//创建具有读/写功能RandomAccessFile对象rd                FileInputStream fis=new FileInputStream(input);//创建 文件读入流对象fis                FileOutputStream fos=new FileOutputStream(output);//创建文件写出流对象fos                try{                    System.out.println("添加文件的内容:");//从键盘上输入信息                    Scanner sc=new Scanner(System.in);                    st=sc.next();                    r.writeBytes(st);//将读入的字符串信息写入RandomAccessFile对象rd                    System.out.println("开始复制文件:"+input);                    do{//将对象fis中的内容写入对象fos,即实现文件内容复制的功能,到文件结尾ir=-1为止                        i=fis.read();                        if(i!=-1){                            fos.write(i);                            System.out.println("...\n");                        }                    }while(i!=-1);//通过DO-while循环,读写文件的内容                        System.out.println(input+"已成功复制到"+output);                        fis.close();//关闭对象fis                        fos.close();//对象fos                }                catch(IOException e)//使用指针用于访问方法printStackTrace()。                {                    e.printStackTrace();                }       }}

运行结果如下:这里写图片描述
1,知识点分析:
文件的操作,文件的概念,文件的路径,文件的名称,字体流的学习
2,心得体会:
自己对文件的操作有了一定的了解