📄 writerandom.java
字号:
import java.io.*;
public class WriteRandom
{
public static void main(String[] args)
throws java.io.IOException
{
String fileName = "products.dat";
String acctstring, amtstring, pricestring;
int acct, amt;
double price;
// set up the keyboard for string input
InputStreamReader isr =
new InputStreamReader(System.in);
BufferedReader br =
new BufferedReader(isr);
// set up the basic random access input/output stream
RandomAccessFile raf =
new RandomAccessFile(fileName, "rw");
// enter and store data for five records
for(int i = 1; i <= 5; i++)
{
System.out.print("Enter the identification number: ");
acctstring = br.readLine();
acct = Integer.parseInt(acctstring);
raf.writeInt(acct);
System.out.print("Enter the quantity in stock: ");
amtstring = br.readLine();
amt = Integer.parseInt(amtstring);
raf.writeInt(amt);
System.out.print("Enter the price: ");
pricestring = br.readLine();
price = Double.parseDouble(pricestring);
raf.writeDouble(price);
}
System.out.println("The file " + fileName + " has been written.");
System.out.println("The length of the file is "
+ raf.length() + " bytes.");
raf.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -