读取文件java.io时出错

时间:2015-01-18 03:26:05

标签: java java-io fileinputstream

所以我试图从文本文件addToLibrary.txt中读取以下字符串

 file:/Users/JEAcomputer/Music/iTunes/iTunes%20Media/Music/Flight%20Of%20The%20Conchords/Flight%20Of%20The%20Conchords/06%20Mutha'uckas.mp3

但是当我这样做时,我得到以下错误:

java.io.FileNotFoundException: file:/Users/JEAcomputer/Music/iTunes/iTunes%20Media/Music/Flight%20Of%20The%20Conchords/Flight%20Of%20The%20Conchords/06%20Mutha'uckas.mp3 (No such file or directory)

奇怪的是我使用这种方法从fileChooser获得了该字符串:

public static void addToLibrary(File f) {

    String fileName = "addToLibrary.txt";

    try {
        FileWriter filewriter = new FileWriter(fileName, true);
        BufferedWriter bufferedWriter = new BufferedWriter(filewriter);
        bufferedWriter.newLine();
        bufferedWriter.write(f.toURI().toString());
        System.out.println("Your file has been written");
        bufferedWriter.close();
    } catch (IOException ex) {
        System.out.println(
            "Error writing to file '"
            + fileName + "'");
    } finally {
    }
}

更奇怪的错误是我的文件阅读器可以读取另一个文件夹中的内容,但不能读取iTunes Media中的任何内容。 我尝试使用以下方法读取不同文件夹中的所有文件:

public void getMusicDirectory() {
    int index = 0;
    try {


        File[] contents = musicDir.listFiles();
        //System.out.println(contents[3].toString());
        for (int i = 0; i < contents.length; i++) {
            //System.out.println("----------------------------------------"+contents.length);
            String name = contents[i].getName();
            //System.out.println(name);

            if (name.indexOf(".mp3") == -1) {
                continue;
            }

            FileInputStream file = new FileInputStream(contents[i]);
            file.read();
            System.out.println(contents[i].toURI().toString());
            songsDir.add(new Song((new MediaPlayer(new Media(contents[i].toURI().toString()))), contents[i]));
            file.close();
        }
    } catch (Exception e) {
        System.out.println("Error -- " + e.toString());
    }
    try(BufferedReader br = new BufferedReader(new FileReader("addToLibrary.txt"))) {

        //System.out.println("In check login try");
        for (String line; (line = br.readLine()) != null; ) {
            FileInputStream file = new FileInputStream(new File(line));
            file.read();
            songsDir.add(new Song(new MediaPlayer(new Media(line)), new File(line)));
            file.close();
        }
        // line is not visible here.
    } catch (Exception e) {
        System.out.println("Error reading add to library-- " + e.toString());
    }


} 

那么我怎样才能做到这一点?为什么方法的第一部分有效但不是第二部分?

2 个答案:

答案 0 :(得分:1)

阅读字符串

时没有问题
file:/Users/JEAcomputer/Music/iTunes/iTunes%20Media/Music/Flight%20Of%20The%20Conchords/Flight%20Of%20The%20Conchords/06%20Mutha'uckas.mp3

来自文件。那部分工作正常。您的问题是在此之后,当您尝试使用路径打开文件时:

file:/Users/JEAcomputer/Music/iTunes/iTunes%20Media/Music/Flight%20Of%20The%20Conchords/Flight%20Of%20The%20Conchords/06%20Mutha'uckas.mp3

因为那不是一条路径;它是一个URI(尽管它可以转换为路径)。

可以将其转换为路径,以便打开它,但你没有理由 - 你的代码实际上没有从文件中读取(除了第一个字节,它没有做任何事情)因此没有必要打开它。删除以下行:

FileInputStream file = new FileInputStream(contents[i]); // THIS ONE
file.read(); // THIS ONE
System.out.println(contents[i].toURI().toString());
songsDir.add(new Song((new MediaPlayer(new Media(contents[i].toURI().toString()))), contents[i]));
file.close(); // THIS ONE

FileInputStream file = new FileInputStream(new File(line)); // THIS ONE
file.read(); // THIS ONE
songsDir.add(new Song(new MediaPlayer(new Media(line)), new File(line)));
file.close(); // THIS ONE

答案 1 :(得分:0)

file:/Users/JEAcomputer/Music/iTunes/iTunes%20Media/Music/Flight%20Of%20The%20Conchords/Flight%20Of%20The%20Conchords/06%20Mutha'uckas.mp3不是有效的File引用,尤其是在Windows下。

由于您已将String标识为URI,因此您应该将其视为......

URI uri = URI.create("file:/Users/JEAcomputer/Music/iTunes/iTunes%20Media/Music/Flight%20Of%20The%20Conchords/Flight%20Of%20The%20Conchords/06%20Mutha'uckas.mp3");

好的,但是,没有真正的方法可以阅读URI,但您可以阅读URL,因此我们需要将URI转换为URL幸运的是,这很简单......

URL url = uri.toURL();

从那里你可以使用URL#openStream打开InputStream(你可以将其包裹在InputStreamReader中)并阅读文件的内容,例如......

String imageFile = "file:/...";
URI uri = URI.create(imageFile);
try {
    URL url = uri.toURL();
    try (InputStream is = url.openStream()) {

        byte[] bytes = new byte[1024 * 4];
        int bytesRead = -1;
        int totalBytesRead = 0;
        while ((bytesRead = is.read(bytes)) != -1) {

            // Somthing, something, something, bytes
            totalBytesRead += bytesRead;

        }

        System.out.println("Read a total of " + totalBytesRead);

    } catch (IOException ex) {
        ex.printStackTrace();
    }
} catch (MalformedURLException ex) {
    ex.printStackTrace();
}

但是,您可以为自己保存很多问题并停止使用f.toURI().toString());File#toURI#toString)之类的内容,而只需使用File#getPath代替......这样您就可以从File ...

创建新的String引用

此外,您的资源管理需要一些工作,基本上,如果您打开它,您应该关闭它。有关更多提示,请参阅The try-with-resources Statement