skriv doubles til fil , bliver til volapyk i filen!
Jeg forsøger, at skrive doubles til en fil, men får volapyk i filen! Jeg har forsøgt at fejlfinde med at få udskrevet position og limit på bytebufferen. (jeg er først ved at lære java, så det undrer mig ikke hvis det er en 'dum' fejl (hvis ikke alle er det!))Jeg har indsat koden nedenfor
package writetofile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;//this is necessary to use other buffes that ByteBuffer
import java.nio.channels.FileChannel;
public class writeToFile {
public static void main(String[] args){
double[] rawData = {1.23, 5.7634, 2.873, 8.093, 2.12, 2.144};//this is our data we want to save to the file
int sizeByteBuffer = rawData.length*8;//for memory buffer allocation. because each element in rawData occupies 8 byte
ByteBuffer rawDataByteBuf = ByteBuffer.allocate(sizeByteBuffer);//buffer of rawData.length capacity
DoubleBuffer rawDataDoubleBuf = rawDataByteBuf.asDoubleBuffer();//creates a view buffer
//load data to buffer
System.out.println("Before data is written to file:\n" +
"Double buffer capacity: " +rawDataDoubleBuf.capacity()+
"\n Byte buffer capacity: " +rawDataByteBuf.capacity()+
"\n Double buffer position: " + rawDataDoubleBuf.position() +
"\n Byte buffer position: " + rawDataByteBuf.position() +
"\n Double buffer Limit: " + rawDataDoubleBuf.limit()+
"\n Byte buffer limit: " + rawDataByteBuf.limit());
rawDataDoubleBuf.put(rawData);
System.out.println("\n\nAfter data is written to buffer:\n" +
"Double Buffer capacity: " +rawDataDoubleBuf.capacity()+
"\n Bytebuffer capacity: " +rawDataByteBuf.capacity()+
"\n Double buffer position: "+ rawDataDoubleBuf.position() +
"\n Byte Buffer position " + rawDataByteBuf.position() +
"\n Double buffer limit: " + rawDataDoubleBuf.limit()+
"\n Byte buffer limit " +rawDataByteBuf.limit());
rawDataByteBuf.position(8*rawDataDoubleBuf.position());//The ByteBuffer is still in its original state. This update the bytebuffer to correspond to the data loaded into the view buffer. This updated position is used to set the limit by the flip method
System.out.println("\n\n After update of position: " + rawDataByteBuf.position());
rawDataByteBuf.flip();//limit for the byte buffer needs to be set to the current position, and the position needs to be set back to 0. flip sets the limit to the current position and position to 0
System.out.println("\n\nAfter flip:\n " +
"Byte Buffer limit: " +rawDataByteBuf.limit()+
"\n Byte Buffer position: " + rawDataByteBuf.position()+
"\n Double buffer limit: " +rawDataDoubleBuf.limit()+
"\n Double buffer position: " +rawDataDoubleBuf.position());
String dirname = new String("C:/stien");
String filename = new String("MyJavaTestFile.txt");
File dir = new File(dirname);//file object
//file stream is created
File aFile = new File(dir, filename);//file object for the file path
FileOutputStream outputFile = null;//place to store the stream reference
try{
outputFile = new FileOutputStream(aFile, true);
System.out.println("\nFile stream created succesfully.("+ aFile+")");
}
catch(FileNotFoundException e){
e.printStackTrace(System.err);
}
//create the file output stream channel and the buffer
FileChannel outChannel = outputFile.getChannel();
//write file
try{
outChannel.write(rawDataByteBuf);
outputFile.close();
System.out.println("\n Buffer contents written to file."+dirname);
}
catch(IOException e){
e.printStackTrace(System.err);
}
System.exit(0);//correct termination
}
}
På forhånd tak for hjælpen.
/Jacob