java-בעיה בקריאה מקובץ - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

java-בעיה בקריאה מקובץ


bikram

Recommended Posts

שלום,

אני אמור לקרוא מקובף טקסט מספרים (כ-int) ואז לשלוח אותם לפונקציה שכבר כתבתי שמכווצת את הקובץ....

בחרתי להשתמש באובייקט של: BufferedReader br

כאשר כל פעם אני קורא מחרוזת- ממיר אותה לint ושולח לפונקציה המכווצת write(int) כפי שניתן לראות:

while(s!=null)
{
int num=Integer.parseInt(s);
cos.write(num);
s=br.readLine();

}

הבעיה היא ש- s (ממחלקת String ) למרות שמקבל null בסוף הקובץ- משום מה עדיין יש כניסה לתוך גוף הלולאה!

אשמח לכל הצעה

תודה

קישור לתוכן
שתף באתרים אחרים

אני מקווה שזה מספיק...

public static void main(String []args) //throws IOException
{
try{
int NumberOfBits; //num of bits- read it from first line in txt file
String s=null; //we read strings from txt file and then parse them to integers

//String inputfile=args[0];
//String outputfile=args[1];

InputStream is= new BufferedInputStream(new FileInputStream(/*inputfile*/"input.txt"));
OutputStream os= new BufferedOutputStream(new FileOutputStream(/*outputfile*/"output.compact"));

BufferedReader br = new BufferedReader(new InputStreamReader(is));
s=br.readLine();
NumberOfBits=Integer.parseInt(s);

CompactOutputStream cos= new CompactOutputStream(os,NumberOfBits);
s=br.readLine();
while(s!=null)
{
int num=Integer.parseInt(s);
cos.write(num);
s=br.readLine();
}
cos.close();
br.close();
}catch(IOException e)
{
System.err.println("an error occured");
}

קישור לתוכן
שתף באתרים אחרים

  • 2 שבועות מאוחר יותר...

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...