java使用io文件输出内存溢出_java - 读取文件java.io时出错 - 堆栈内存溢出

所以我正在尝试从文本文件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());

}

}

那我该如何做呢? 为什么方法的第一部分起作用而第二部分却不起作用?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值