2013-12-08 4 views
1

내 프로그램은 기본적으로 일일 플래너입니다.자바에서 ObjectInputStream을 사용하여 파일에서 문자열 배열 검색?

일정은 ObjectOutputStream에 의해 월 및 연도별로 파일에 저장됩니다. 확인

일정은 하루 단위로 배열됩니다. 확인

예약은 ObjectInputStream에 의해 검색됩니다. 이것은 내가 문제가있는 곳입니다.

public class Calendar { 
public String date; 
public String[] schedule = new String[31]; 
Calendar(){ 


} 

public String retrieve(int month, int day, int year) { 
    date = Integer.toString(month) + "-"+ Integer.toString(year) + ".txt"; 

    try { 
     ObjectInputStream input = new ObjectInputStream(new 
FileInputStream(date)); 
     input.readObject(); 
     schedule = input;  
//This is where I have the error obviously schedule is a string array and 
//input is an ObjectInputStream so this wont work 
     input.close(); 
     return schedule[day-1]; 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return "File not found"; 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return "IOException"; 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return "ClassNotFound"; 
    } 

} 

public void save(int month, int day, int year, JTextArea entry) { 
    date = Integer.toString(month) + "-"+ Integer.toString(year) + ".txt"; 
    schedule[day-1]= entry.getText(); 
    try { 
     ObjectOutputStream output = new ObjectOutputStream(new 
FileOutputStream(date)); 
     output.writeObject(schedule); 
     output.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 



} 

일정

확인

entry.setText(calendar.retrieve(month,day,year)); 

답변

5
ObjectInputStream input = new ObjectInputStream(new FileInputStream(date)); 

같은 것을 사용하여 텍스트 영역에 표시됩니다.

input.readObject(); 

무의미한. 이 메서드는 읽은 개체를 반환합니다. 변수에 저장해야합니다.

schedule = input; 

또한 무의미합니다. input은 곧 닫을 약 ObjectInputStream입니다. 그것을 다른 변수에 저장하는 것은 쓸데있다.

//This is where I have the error obviously schedule is a string array and 
//input is an ObjectInputStream so this wont work 
input.close(); 

이 있어야한다

schedule = (String[])input.readObject(); 
관련 문제