我有一个文件要读取,就像这样
mytxt.txt
1234 http://www.abc.com
8754 http://www.xyz.com
我试过这个
try {
// make a 'file' object
File file = new File("e:/mytxt.txt");
// Get data from this file using a file reader.
FileReader fr = new FileReader(file);
// To store the contents read via File Reader
BufferedReader br = new BufferedReader(fr);
// Read br and store a line in 'data', print data
String data;
while((data = br.readLine()) != null)
{
//data = br.readLine( );
System.out.println(data);
}
} catch(IOException e) {
System.out.println("bad !");
}
我用过这个,但实际的问题是我想逐个阅读这两个字符,然后将数字添加到链接中,我将其读作字符串。 任何人都可以告诉我我是怎么想的那样......? 任何帮助将不胜感激。
答案 0 :(得分:0)
这是你想要的吗?
while((data = br.readLine()) != null)
{
String[] data=br.readLine().split();
if(data!=null&&data.length==2)
{
System.out.println(data[1]+"/"+data[0]);
}else
{
System.out.println("bad string!");
}
}
答案 1 :(得分:0)
解析您正在阅读的行,搜索第一个空格(我假设您只有一个空格分隔您的数字和您的网址),如下所示:
try {
// make a 'file' object
File file = new File("e:/mytxt.txt");
// Get data from this file using a file reader.
FileReader fr = new FileReader(file);
// To store the contents read via File Reader
BufferedReader br = new BufferedReader(fr);
// Read br and store a line in 'data', print data
String data;
while((data = br.readLine()) != null)
{
int posWhite = data.indexOf(' ');
String digit = data.substring(0, posWhite);
String url = data.substring(posWhite + 1);
System.out.println(url + "/" + digit);
}
} catch(IOException e) {
System.out.println("bad !");
}
答案 2 :(得分:0)
在while((data = br.readLine())!= null)中,制作如下代码:
String tmpData[] = data.split(" ");
System.out.println(tmpData[1] + "/" + tmpData[0]);