나는 안드로이드 게임을위한 간단한 레벨 편집기를 만들고있다. 스윙을 사용하여 GUI (그리드 그리기)를 작성했습니다. 타일을 배치하려는 사각형을 클릭하면 색상이 바뀝니다. 일단 끝나면 모든 것을 파일에 씁니다.파일에서 정수 값 읽기 (Java)
내가 읽기를 중지 독자에게 읽는 수준 번호와 하이픈을 결정하기 위해 별표를 사용
내 파일 (이것은 단지 예입니다)는 다음과 같이 구성 .
내 파일 읽기 코드은 아래에서 읽을 부분을 선택하면됩니다. 예를 들어. 내가 수행하여 2 통과하면 다음
이readFile(2);
그럼 내가 '시작했으면 내가 알아낼 수없는 것을 제 2 섹션
에 모든 문자를 인쇄 '요점, 실제로 의 정수로 숫자를 읽으려면 및 이 아닌 개 문자를 각각 읽으시겠습니까?
코드
public void readFile(int level){
try {
//What ever the file path is.
File levelFile = new File("C:/Temp/levels.txt");
FileInputStream fis = new FileInputStream(levelFile);
InputStreamReader isr = new InputStreamReader(fis);
Reader r = new BufferedReader(isr);
int charTest;
//Position the reader to the relevant level (Levels are separated by asterisks)
for (int x =0;x<level;x++){
//Get to the relevant asterisk
while ((charTest = fis.read()) != 42){
}
}
//Now we are at the correct read position, keep reading until we hit a '-' char
//Which indicates 'end of level information'
while ((charTest = fis.read()) != 45){
System.out.print((char)charTest);
}
//All done - so close the file
r.close();
} catch (IOException e) {
System.err.println("Problem reading the file levels.txt");
}
}
고마워요! 위대한 작품 :-) – Zippy