如何写入USB(外部存储)

时间:2015-08-21 19:29:09

标签: java file usb

我正在尝试将文件从我的电脑复制到USB中的USB。问题是,代码在Usb上创建了一个文件,但文件的内容没有从源复制到目标(计算机到USB)。我怎样才能做到这一点? 我希望将文件名R.java作为system.txt复制到usb中。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class R {

private static File to;

public static void main(String[] args) {
    // current working dir where there is file u want to replicate
    File f = new File("." + "/R.java");

    // destination location
    File so = new File("/media");
    for (File s : so.listFiles()) {
        String r = s.getName();
        to = new File("/media/" + r + "/system.txt");
        if (!to.exists()) {
            try {
                to.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out
                        .print(to.getName() + " " + e.getMessage() + "\n");
            }
        }
            if (f.exists()) {
                FileChannel is = null;
                FileChannel os = null;
                try {
                    is = new FileInputStream(f).getChannel();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    os = new FileOutputStream(to).getChannel();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    os.transferFrom(is, 0, is.size());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

 }

我在LINUX机器上

0 个答案:

没有答案