Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 读取文件中的特定信息_Java_File - Fatal编程技术网

Java 读取文件中的特定信息

Java 读取文件中的特定信息,java,file,Java,File,我想用扫描器读取这个文件,但不是所有的信息,只有分号后面的东西,比如10,warrior,John Smith等等 currhp: 10 type: Warrior name: John Smith items: Stick,1,2,10,5 gold: 10 type: Wizard 我试图解决它,但我做不到 扫描仪填充; 扫描仪inp=新扫描仪(System.in); 试一试{ infle=新扫描仪(新文件(“player_save.txt”); }捕获(FileNo

我想用扫描器读取这个文件,但不是所有的信息,只有分号后面的东西,比如10,warrior,John Smith等等

currhp: 10

type: Warrior

name: John Smith

items:
      Stick,1,2,10,5

gold: 10

type: Wizard
我试图解决它,但我做不到

扫描仪填充;
扫描仪inp=新扫描仪(System.in);
试一试{
infle=新扫描仪(新文件(“player_save.txt”);
}捕获(FileNotFoundException o){
System.out.println(o);return;}
而(infle.hasNextLine()){
System.out.println(infle.nextLine());
}

您需要打开文件,逐行读取,然后拆分每一行,并使用希望对其进行进一步处理的行块

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile{
    public static void main(String[] args) throws IOException {
        //fix the URL to where your file is located.
        try (BufferedReader br = new BufferedReader(new FileReader("C:\\player_save.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                if(line.length() > 0) {
                      if(line.contains(":")) {
                          System.out.println("Before colon >> " + line.split(":")[0]);  
                       } else {
                          //no column found taking the whole line
                         System.out.println("whole line having no coln: " + line);                    
                     }
                }
            }
        } catch(FileNotFoundException fnfe) {   
          //Handle scenario where the file does not exist   
        } catch(IOException ie) {
          //Handle other File I/O exceptions here
        }
    }
}

你可以这样做-

List<String> list= new ArrayList<>();
    try(
    Scanner scan= new Scanner(new BufferedReader(new FileReader("//your filepath")));
    ){      

    while(scan.hasNextLine()){
        String s=scan.nextLine();
        if(s.contains(":")){
            String arr[]=s.split(":");
            list.add(arr[1].trim());
            }
        }
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
    for(String str:list){
        System.out.println(str);
    }
List List=new ArrayList();
试一试(
Scanner scan=new Scanner(new BufferedReader(new FileReader(“//您的文件路径”));
){      
while(scan.hasNextLine()){
字符串s=scan.nextLine();
如果(s)包含(“:”){
字符串arr[]=s.split(“:”);
添加(arr[1].trim());
}
}
}
捕获(例外e){
System.out.println(e.getMessage());
}
for(字符串str:list){
系统输出打印项次(str);
}

您能否提供有关该问题的更多详细信息?你有一些错误信息吗?您的问题与Javascript有何关系?我想读取此文件中的信息,但不在该符号后添加字符串:“不带(currhp:,type:,name:,items:,gold:,type:),因为我想将这些信息放在一个类中,以便以后使用它们。我希望这个想法已经到达您的手中。为此,添加了+1。包含(“:”)验证。可能还有其他情况,但他的问题并不清楚。更新仍然失败是什么意思?您编辑了问题,但忘记添加line.contains(“:”)或其他相关内容。但现在你有了。干得好干杯,既然你删除了你的+1,我还是会给你另一个例子,因为它指向java.lang.ArrayIndexOutOfBoundsException:)我已经放的cod没有错误,但我的意思是我尝试了很多次,但都没能解决。为了让问题更清楚,我想读取此文件中的信息,但此符号后不带字符串:“不带(currhp:,type:,name:,items:,gold:,type:),将其放入类中,稍后使用。我已更新代码,将冒号左侧的字符串打印出来。基本上,我首先检查以确保该行包含文本,如果是,则检查它是否包含冒号。如果该行包含冒号,则我使用“:”将其拆分,并返回数组的第一个元素,该元素将是冒号左侧的值。如果没有找到冒号,则返回整行的打印结果。您应该能够根据需要修改代码。