如何在Java中从文件读取字符串时跳过一个字符

huangapple go评论105阅读模式
英文:

how to skip a character when read strings from a file in Java

问题

例如,文件的内容如下:

black=white

bad=good

easy=hard

因此,我希望将这些词存储在一个映射中,其中词作为键和值(例如:{black=white, bad=good})。我的问题是,当我读取字符串时,我必须跳过一个字符'=',这会使键和值消失。如何实现这个?

在下面的代码中,我编写了一个从文件中读取键和值的代码,但这个代码只在单词之间有空格时才有效,但我需要'='。

System.out.println("文件名:");
String pathToFile = in.nextLine();
File cardFile = new File(pathToFile);
try(Scanner scanner = new Scanner(cardFile)){
    while(scanner.hasNext()) {
        key = scanner.next();
        value = scanner.next();
        flashCards.put(key, value);
    }
}catch (FileNotFoundException e){
    System.out.println("未找到文件:" + pathToFile);
}
英文:

For example, the content of a file is:

black=white

bad=good

easy=hard

So, I want to store in a map this words as key and value (ex: {black=white, bad=good} ). And my problem is when I read string I have to skip a char '=' which disappears key and value. How to make this?

In code below I make a code which read key and value from file, but this code works just when between words is SPACE, but I have to be '='.

System.out.println("File name:");
    String pathToFile = in.nextLine();
    File cardFile = new File(pathToFile);
    try(Scanner scanner = new Scanner(cardFile)){
        while(scanner.hasNext()) {
            key = scanner.next();
            value = scanner.next();
            flashCards.put(key, value);
        }
    }catch (FileNotFoundException e){
        System.out.println("No file found: " + pathToFile);
    }

答案1

得分: 3

使用Java中的Stringsplit方法。

所以在读取你的行之后,分割字符串并取键和值如下。

String[] keyVal = line.split("=");
System.out.println("key is ", keyVal[0]);
System.out.println("value is ", keyVal[1]);
英文:

Use the split method of String in Java.

so after reading your line, split the string and take the key and value as so.

String[] keyVal = line.split("=");
System.out.println("key is ", keyVal[0]);
System.out.println("value is ", keyVal[1]);

答案2

得分: 1

你可以更改扫描器的分隔符。

public static void main(String[] args) throws java.lang.Exception {
    String s = "black=white\nbad=good\neasy=hard";
    Scanner scan = new Scanner(s);
    scan.useDelimiter("\\n+|=");
    while (scan.hasNext()) {

        String key = scan.next();
        String value = scan.next();
        System.out.println(key + ", " + value);
    }
}

输出:

black, white
bad, good
easy, hard

更改分隔符可能会有些棘手,最好的方法可能是读取每行,然后解析它。例如,\\n+|= 将通过一个或多个换行符或等号来分割标记。然而,换行符在某种程度上是硬编码的,所以它可能会根据创建文件的平台而变化。

英文:

You can change the delimiter for the scanner.

public static void main (String[] args) throws java.lang.Exception
{
	String s = "black=white\nbad=good\neasy=hard";
	Scanner scan = new Scanner(s);
	scan.useDelimiter("\\n+|=");
	while(scan.hasNext()){
		
		String key = scan.next();
		String value = scan.next();
		System.out.println(key + ", " + value);
	}
}

The output:

>black, white
bad, good
easy, hard

Changing the delimiter can be tricky, and it could be better to just read each line,then parse it. For example, "\\n+|=" will split the tokens by either one or more endlines, or an "=". The end line is somewhat hard coded though so it could change depending on the platform the file was created on.

答案3

得分: -1

A simple "if" condition will solve it.

if (key == '='){ break;}

英文:

A simple "if" condition will solve it.

if (key == '='){ break;}

huangapple
  • 本文由 发表于 2020年7月22日 14:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63028206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定