zoukankan      html  css  js  c++  java
  • 使用IO技术,创建一个目录,然后复制一个文件到该目录!实现复制的功能。(在博客园上传的第一份代码)

    import java.io.*;

    /**
    * @author lyx
    * 2014-5-12 下午05:09:57
    * 获取文件夹 类
    * FileInputStream-- 文件输入流 从文件中读取数据到内存中 , 用read()方法读取数据
    * FileOutputStream-- 文件输出流 向文件中写入数据,用write()方法写入数据
    */

    public class Directory {

    /**
    * @param args
    */
    public static void main(String[] args) {

    File file=new File("lyx\lyx.txt");//create a .txt-file
    File f=new File("lyx1.txt");
    FileInputStream fis;//声明一个文件输入流对象
    FileOutputStream fos;//声明一个文件输出流对象
    //String s="i have a dream!!!";

    try {
    //读取文件
    fis=new FileInputStream(file);//通过FileInputStream类 把文件中的数据读到内存中
    byte[] by1=new byte[(int)(file.length())];//定义一个字节数组by1[],长度为文件的长度
    fis.read(by1); //通过read()读取文件中的数据,把数据存储到字节数组by1中,并返回读取字节的数量
    String s1=new String(by1); //通过String方法把字节数组转化为一个字符串



    //输出文件
    fos=new FileOutputStream(f,true);//通过FileOutputStream类 把内存中的数据写入到文件中去

    /* String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示*/
    byte[] by= s1.getBytes();//将字符串s1转化字节并存储在字节数组中
    fos.write(by);//以字节为单位把数据写入文件中

    } catch (Exception e) {

    e.printStackTrace();
    }

    }

    }

  • 相关阅读:
    Unique path
    *Jump Game
    Valid Palindrome
    *Reverse Words in a String
    Min Stack
    [?]*Simplify Path
    *Valid Parentheses
    *Sqrt(x)
    String to Integer (atoi)
    Add Digits
  • 原文地址:https://www.cnblogs.com/lovelyx/p/3726334.html
Copyright © 2011-2022 走看看