[英]How to write and read dat file
Write a program that asks the user to enter whole numbers (they enter –1 to finish data entry).
编写一个程序,要求用户输入整数(他们输入–1以完成数据输入)。 The numbers are to be written to a binary file.
这些数字将被写入二进制文件。 This is to be done in a method called
writeBinaryFile()
.这可以通过称为
writeBinaryFile()
的方法来完成。 Then the program should read the numbers from the binary file and display them to the console.然后,程序应从二进制文件中读取数字并将其显示在控制台中。 This should be done in a method called
readBinaryFile()
.这应该在称为
readBinaryFile()
的方法中完成。 Themain()
method should just callwriteBinaryFile()
thenreadBinaryFile()
.main()
方法应该只调用writeBinaryFile()
然后再调用readBinaryFile()
。
My code so far: 到目前为止,我的代码:
public static void main(String[] args) throws FileNotFoundException, IOException {
System.out.println("Please enter whole numbers then enter -1 to data entry.");
writeBinaryFile();
readBinaryFile();
}
/**
* This method opens a binary file and writes the contents
* of an int array to the file.
*/
private static void writeBinaryFile() throws IOException, FileNotFoundException
{
Scanner input = new Scanner(System.in);
ArrayList<Integer> number = new ArrayList<Integer>();
try
{
FileOutputStream output = new FileOutputStream("Files//Numbers.dat");
int numbers = 0;
while(numbers != -1)
{
number.add(numbers = input.nextInt());
output.write(numbers);
}
output.close();
}
catch(IOException ex)
{
ex.getMessage();
}
}
private static void readBinaryFile() throws IOException
{
FileInputStream input = new FileInputStream("Files//Numbers.dat");
int value;
value = input.read();
System.out.print("The numbers in the file are: ");
try
{
while(value != -1)
{
System.out.print(value +" ");
value = input.read();
}
input.close();
}
catch(IOException ex)
{
ex.getMessage();
}
}
The problem is that when I enter this data: 问题是当我输入此数据时:
5 2 4 6 8 4 -1
5 2 4 6 8 4 -1
My output is: 我的输出是:
5 2 4 6 8 4 255
5 2 4 6 8 4 255
How can I stop 255 from coming up? 如何阻止255出现?
You should avoid adding -1 to input. 您应该避免在输入中加上-1。 The problem is at line:
number.add(numbers = input.nextInt());
问题出在以下行:
number.add(numbers = input.nextInt());
in loop. 循环中。
EDIT: To write binary data you should use DataInputStream/DataOutputStream
. 编辑:要写入二进制数据,您应该使用
DataInputStream/DataOutputStream
。 You cannot mix it with Scanner
as that is primarily for text data. 您不能将其与
Scanner
混合使用,因为它主要用于文本数据。 A sample example is: 一个示例示例是:
public static void main(String[] args) throws IOException {
writeNumbers();
readNumbers();
}
private static void writeNumbers() throws IOException {
DataOutputStream output = new DataOutputStream(new FileOutputStream("C://Numbers.dat"));
for (int i = 0; i < 10; i++) {
output.writeInt(i);
System.out.println(i);
}
output.close();
}
private static void readNumbers() throws IOException{
DataInputStream input = new DataInputStream(new FileInputStream("C://Numbers.dat"));
while (input.available() > 0) {
int x = input.readInt();
System.out.println(x);
}
input.close();
}
try doing like this... 尝试像这样...
while(numbers = input.nextInt())!=-1)
{
number.add(numbers);
output.write(numbers);
}
Just update @akhul_mittal answer to include reading from standard input using Scanner. 只需更新@akhul_mittal答案即可包括使用扫描仪从标准输入中读取的内容。
public static void main(String[] args) throws IOException {
writeNumbers();
readNumbers();
}
private static void writeNumbers() throws IOException {
DataOutputStream output = new DataOutputStream(new FileOutputStream("C://Numbers.dat"));
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (n != -1) {
output.writeInt(n);
n = scanner.nextInt();
}
output.close();
}
private static void readNumbers() throws IOException {
DataInputStream input = new DataInputStream(new FileInputStream("C://Numbers.dat"));
while (input.available() > 0) {
int x = input.readInt();
System.out.println(x);
}
input.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.