【发布时间】:2014-01-26 19:19:09
【问题描述】:
嗨,在 java 中,我正在尝试拆分一个文本文件 [data.txt],其中包含:
abc.txt
hello.jpg
play.mp4
image.jpg
text.txt
...
文本格式的文件名。现在我想使用java程序根据.mp3、.txt、.jpg等文件扩展名分割这个文件。因为稍后我想根据扩展名或文件类型使用不同的程序执行这些文件。
我已经创建了一个示例程序,但我不知道如何根据扩展名对其进行拆分
示例程序:
import java.util.*;
import java.io.*;
class RF
{
public void readFile()
{
try
{
FileInputStream fstream = new FileInputStream("data.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine())!=null)
{
String[] splitted = strLine.split(" ");
for( String s : splitted)
{
System.out.println(s);
}
}
in.close();
} catch(Exception e)
{
}
}
}
public class FileSplit
{
public static void main(String args[])
{
RF r = new RF();
r.readFile();
}
}
我想输出为:
output
abc is a text file
hello is a image file
play is a mp4 file
image is a image file
text is a text file
谢谢。
【问题讨论】:
-
提示:将
catch (Exception e) { ..形式的代码改为catch (Exception e) { e.printStackTrace(); // very informative! .. -
你想要什么输出?
-
您一直在使用“拆分”一词,但您描述的是过滤。是否要根据模式查找文件中的条目(例如查找以“txt”结尾的文件名)?
-
是的,我想根据模式进行过滤。
标签: java