Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将4位数字附加到从文件读取的下一个字符串_Java_String_File_Append_File Handling - Fatal编程技术网

Java 如何将4位数字附加到从文件读取的下一个字符串

Java 如何将4位数字附加到从文件读取的下一个字符串,java,string,file,append,file-handling,Java,String,File,Append,File Handling,我有一个文件要读,就像这样 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

我有一个文件要读,就像这样

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 !");  
 }  
我用了这个,但实际的问题是,我想一个接一个地读这两个字符,然后把这个数字显示在链接上,我将把它读作字符串。 谁能告诉我该怎么做。。? 任何帮助都将不胜感激。

这就是你想要的吗

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

解析您正在阅读的行,搜索第一个空格(我假设您只有一个空格分隔您的数字和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 tmpData[] = data.split(" ");
System.out.println(tmpData[1] + "/" + tmpData[0]);
在while((data=br.readLine())!=null)中,使代码如下:

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 tmpData[] = data.split(" ");
System.out.println(tmpData[1] + "/" + tmpData[0]);

你的问题不清楚。您能给出一个您需要的输出示例吗?我想要的输出类似于www.abc.com/1234,通过它我可以在selenium webdriver的帮助下导航到该站点。谢谢,我已经了解了这一点,但真正的问题是如何将第一个temData[1]附加到其他temData[0]中,然后一起打印。因为我必须导航到带有结果输出的浏览器,所以得到了答案…String test=newstringbuffer(tmpData[1])。append(tmpData[0])。toString();String.split将是手动搜索字符串的更干净的替代方法。这也是我想到的第一件事,但在一些奇怪的情况下,我发现URL中有空格(Firefox现在允许您键入这样的URL,尽管它在内部“转义”),因此只查找第一个空格将更安全
data.split(“,2)
将处理该情况。