2017-02-07 1 views
1

저는 프로젝트에서 일하고 있는데 도움이 필요합니다. 이건 내 간단한 질문문자열에서 문자열로 문자 복사

1) I라는 문자열에 기록 할 것입니다

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 

:

그래서 여기 내 테스트 코드입니다 ':'(Karlken) 앞의 첫 단어, 다른 문자열 (Java)의 ':'다음 두 번째 단어, ' 결국 또 다른 문자열에 나는 "남성"이라고 쓰고 싶습니다. 어떻게 할 수 있습니까?

+2

'line.split (":")' – shmosel

+0

세 번째 문자열이 혼동 스럽습니다. 단순히 의미하는 것이 아닙니다. String male = "male"; ? –

+0

문자열 문자열 = "C : \\ 사용자 \\ karlk \\ 작업 영역 \\ 작업 \\ src \\ 테스트 \\ tempx.txt"; String [] parts = string.split (":"); 문자열 part1 = 부분 [0]; 문자열 part2 = 부분 [1]; –

답변

0

당신은이 목적을 위해 스캐너를 사용할 수 있습니다 :

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 + "'"); 
    } 
} 
0
while((line = bufferedReader.readLine()) != null) { 
    String text = line; 
    String[] parts = string.split(":"); 
    String part1 = parts[0]; 
    String part2 = parts[1]; 
    String part2 = parts[2]; 
} 

코드에 적합합니다. 파일의 텍스트 형식이 미리 정의되어있는 경우

+0

에 오신 것을 환영합니다! 이 사용자의 문제를 해결했을 수도 있지만, 앞으로는이 문제에 봉착 한 사용자에게는 코드 전용 답변이별로 도움이되지 않습니다. 코드가 원래의 문제를 해결하는 이유를 설명하기 위해 답을 편집하십시오. –

0

(즉 항상 하나의 :에 의해 분리 된 3 개 부분), 다음이 충분해야합니다 :

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

어떻게 "readLineFromFile"을 사용할 수 있습니까? 그것은 나에게 오류를 준다. – Karlken

+0

@yinon 존 클린의 대답에서 코드를 복사 한 것처럼 보입니다. – GriffeyDog

+0

@GriffeyDog 그것은 다른 방법입니다. john의 게시물에 대한 [history] (http://stackoverflow.com/posts/42097986/revisions)를 확인하십시오. – Izruo