【发布时间】:2018-01-19 20:01:26
【问题描述】:
我希望这个程序在我的 txt 文件中写入数字,但它写了一些奇怪的符号。有没有人可以修复它并让它从数组中写入数字
package int1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
public class broj_u_skl {
public static void main(String[] args) {
File a = new File("C:\\Users\\Jovan\\Desktop");
File b = new File(a,"Pisem.txt");
try {
b.createNewFile();
}catch(Exception e) {
}
FileOutputStream st = null;
try {
st = new FileOutputStream(b);
}catch(Exception e) {
}
这是那个数组:
int[] c = {1,2,3,4,5,6,7,8,9};
但上面没有写这个数字。
ByteBuffer bff = ByteBuffer.allocate(100);
FileChannel ch = st.getChannel();
IntBuffer ib = bff.asIntBuffer();
for (int i = 0; i < c.length; i++) {
ib.put(c[i]);
}
bff.position(4*ib.position());
bff.flip();
try {
ch.write(bff);
}catch(IOException e) {
}
}
}
【问题讨论】:
-
写完后关闭
st。
标签: java