In: Computer Science
In writing integers to a file you need to connect 2 streams together to accomplish this task. Below show the code you might use to connect 2 stream classes together. (Use a FileOutputStream and a DataOutputStream)
Code:
public class FileExample {
/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
try{ String
path1="E:\\intwritingex.txt" ;
FileOutputStream fout=new
FileOutputStream(path1); //path1 is file
pathname
DataOutputStream ds = new DataOutputStream(fout); //connectting ds
stream to file stream
ds.writeInt(1);
//writing usig ds as integer not possible in ordinary file much
easily integer is wirtten in as hexacedimal in file not
decimal
ds.flush(); //FLUSH IS
USED FOR FORCED WRITING
ds.close() ;//closoiign
data stream
fout.close();//closing
file steam
System.out.println("operations perfomed succesfully");
}catch(Exception e){System.out.println("FILE CANNOT BE
OPENED");}
}//end of main
}//end of class
Word of caution datastream writeInt writes integer in hexadecimal format to file
Output:
Code ScreenShot