【问题标题】:How to append 4 digit number to the next string read from file如何将 4 位数字附加到从文件中读取的下一个字符串
【发布时间】:2013-08-09 14:53:52
【问题描述】:

我有一个文件要读,是这样的

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 !");  
 }  

我使用了这个,但实际的问题是我想一个一个地读取这两个字符,然后将数字附加到我将作为字符串读取的链接。 谁能告诉我我应该怎么做..? 任何帮助将不胜感激。

【问题讨论】:

  • 你的问题不清楚。你能给出你需要的示例输出吗?
  • 我想要的输出类似于 www.abc.com/1234,我可以使用它在 selenium webdriver 的帮助下导航到该站点。

标签: java string file append file-handling


【解决方案1】:

这是你想要的吗?

   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!"); 
           }
    }   

【讨论】:

    【解决方案2】:

    解析你正在阅读的行,搜索第一个空格(我假设你只有一个空格分隔你的数字和你的 url)是这样的:

    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 !");  
    }  
    

    【讨论】:

    • String.split 将是手动搜索字符串的更简洁的替代方案。
    • 也是我想到的第一件事,但在一些奇怪的情况下,我发现了其中包含空格的 URL(Firefox 现在允许您键入此类 URL,尽管它在内部“转义”)所以只找到第一个空间会更安全
    • data.split(" ", 2) 会处理这种情况。
    【解决方案3】:

    在while((data = br.readLine()) != null)中,把代码写成这样:

    String tmpData[] = data.split(" ");
    System.out.println(tmpData[1] + "/" + tmpData[0]);
    

    【讨论】:

    • 谢谢,我明白了,但真正的问题是如何将第一个 temData[1] 附加到其他 temData[0] 然后一起打印。因为我必须使用结果输出导航到浏览器
    • 得到了答案...String test =new StringBuffer(tmpData[1]).append(tmpData[0]).toString();
    猜你喜欢
    • 2015-03-07
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多