📄 readprimes.java
字号:
import java.io.*;
public class ReadPrimes
{
public static void main(String[] args)
{
try
{
// Create a File object and an input stream object for the file
String directory = "c:\\JunkData\\"; // Directory path
String fileName = "Primes.bin"; // File name
File myPrimes = new File(directory, fileName);
DataInputStream primesIn = new DataInputStream(
new FileInputStream(myPrimes));
// Create a default formatted character output stream
FormatWriter out = new FormatWriter(
new BufferedWriter(
new FileWriter(FileDescriptor.out)));
long[] primes = new long[6]; // Array for one line of primes
boolean EOF = false; // End of file flag
while(!EOF)
{
int index = 0; // Index for storing primes
try
{
// Fill the array with primes from the file
for(index = 0; index < primes.length; index++)
primes[index] = primesIn.readLong();
}
catch(EOFException e)
{
EOF = true; // This will end the while loop
}
// Output the number of primes in the array
for(int j = 0; j < index; j++)
out.print(primes[j]);
out.println(); // Write end of line
}
out.close(); // Flush and close the output stream
primesIn.close(); // Close the input stream
}
catch(FileNotFoundException e) // Stream creation exception
{
System.err.println(e);
return;
}
catch(IOException e) // File read exception
{
System.err.println("Error reading input file" + e );
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -