我在一個項目中工作,我想要一些幫助。將字符串從字符串複製到字符串
因此,這裏是我的測試代碼:
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]; –