我在一个项目中工作,我想要一些帮助。将字符串从字符串复制到字符串
因此,这里是我的测试代码:
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)我想在一个名为字符串写在':'(Karlken)之前命名第一个单词,在另一个字符串(Java)中':'之后的第二个单词,以及最后,再次在另一个字符串中我想写“男”,我该怎么办?
'line.split(“:”)' – shmosel
您的第三个字符串混淆。您不只是指: String male =“male”; 是吗? –
String string =“C:\\ Users \\ karlk \\ workspace \\ Work \\ src \\ test \\ tempx.txt”; String [] parts = string.split(“:”); String part1 = parts [0]; String part2 = parts [1]; –