Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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 - Fatal编程技术网

Java 将字符从字符串复制到字符串

Java 将字符从字符串复制到字符串,java,Java,我在一个项目中工作,我想得到一些帮助 下面是我的测试代码: package test; import java.io.*; public class Main { public static void main(String [] args) { // The name of the file to open. String fileName = "C:\\Users\\karlk\\workspace\\Work\\src\\test\\temp

我在一个项目中工作,我想得到一些帮助

下面是我的测试代码:

 package test;

import java.io.*;

public class Main {
    public static void main(String [] args) {

        // The name of the file to open.
        String fileName = "C:\\Users\\karlk\\workspace\\Work\\src\\test\\tempx.txt";
        // This will reference one line at a time
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = 
                new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }   

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
            // Or we could just do this: 
            // ex.printStackTrace();
        }
    }
}
tempx.txt

Karlken:Java:Male
这是我的简单问题

1) 我想在一个名为“name”的字符串中写入“:”(Karlken)前面的第一个单词,在另一个字符串(Java)中写入“:”之后的第二个单词,最后,我想在另一个字符串中写入“Male”,我怎么能这样呢

while((line = bufferedReader.readLine()) != null) {
  String text = line;
  String[] parts = string.split(":");
  String part1 = parts[0];
  String part2 = parts[1];
  String part2 = parts[2];
}
似乎更适合你的代码


似乎更适合您的代码。

如果文件中的文本格式是预定义的(即始终由单个
分隔的3个部分),那么以下内容就足够了:

String text = readLineFromFile(filepath);
String[] parts = text.split(":");
String name = parts[0];
String lang = parts[1];
String gender = parts[2];

如果文件中的文本格式是预定义的(即始终由单个
分隔的3个部分),则以下内容就足够了:

String text = readLineFromFile(filepath);
String[] parts = text.split(":");
String name = parts[0];
String lang = parts[1];
String gender = parts[2];

为此,您可以使用扫描仪:

public static void main(String[] args){
    String fileName = "C:\\Users\\karlk\\workspace\\Work\\src\\test\\tempx.txt";

    try(Scanner scanner = new Scanner(new File(fileName))){
        scanner.useDelimiter(":");

        String name = scanner.next();
        String lang = scanner.next();
        String sex = scanner.next();            
    }catch(FileNotFoundException ex) {
        System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
    }catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");
    }
}

为此,您可以使用扫描仪:

public static void main(String[] args){
    String fileName = "C:\\Users\\karlk\\workspace\\Work\\src\\test\\tempx.txt";

    try(Scanner scanner = new Scanner(new File(fileName))){
        scanner.useDelimiter(":");

        String name = scanner.next();
        String lang = scanner.next();
        String sex = scanner.next();            
    }catch(FileNotFoundException ex) {
        System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
    }catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");
    }
}

line.split(“:”)
被第三个字符串弄糊涂了。你的意思不仅仅是:stringmale=“male”;是吗?String String=“C:\\Users\\karlk\\workspace\\Work\\src\\test\\tempx.txt”;String[]parts=String.split(“:”);字符串part1=零件[0];字符串part2=零件[1]@johncliffe我想要的是一个txt文件中的字符串name=Karlken String lang=Java String sex=Male,其中的第三个字符串写为“Karlken:Java:Male”
line.split(“:”
)。你的意思不仅仅是:stringmale=“male”;是吗?String String=“C:\\Users\\karlk\\workspace\\Work\\src\\test\\tempx.txt”;String[]parts=String.split(“:”);字符串part1=零件[0];字符串part2=零件[1]@johncliffe我想要的是一个txt文件中的字符串name=Karlken String lang=Java String sex=Male,其中写着“Karlken:Java:Male”@yinon看起来像是你从john cliffe的答案中复制的代码。@GriffeyDog相反。查看john的帖子。@Karlken这是pseodo代码,这意味着您必须用工作代码替换它,即
bufferedReader.readLine()
,正如您在提问时所写的那样。@yinon看起来您是从john cliffe的答案中复制了代码。@GriffeyDog则相反。查看john的帖子。@Karlken这是pseodo代码,意味着您必须用工作代码替换它,即
bufferedReader.readLine()
,正如您在提问时所写。欢迎使用堆栈溢出!虽然您可能已经解决了该用户的问题,但仅使用代码的答案对将来遇到此问题的用户没有多大帮助。请编辑您的答案,解释为什么您的代码解决了原始问题。欢迎使用堆栈溢出!虽然您可能已经解决了该用户的问题,但仅使用代码的答案对将来遇到此问题的用户没有多大帮助。请编辑您的答案,解释您的代码解决原始问题的原因。