package Task4;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
//import com.sun.java_cup.internal.runtime.virtual_parse_stack;
public class Task4 extends JFrame{
static JTextArea ta = new JTextArea();
static JTextField tf1 = new JTextField();
static JTextField tf2 = new JTextField();
static JTextField tf3 = new JTextField();
/**
* @param title
*/
public Task4(String title) {
super(title);
JScrollPane jp = new JScrollPane(ta);
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jp.setViewportView(ta);
jp.setPreferredSize(new Dimension(700,600));
add(jp);
setLocation(300,300);
setSize(500, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("创建文件目录");
JLabel label2 = new JLabel("创建文件大小");
JLabel label3 = new JLabel("创建耗时");
Button button = new Button("创建");
button.addActionListener(new FileImplement());
tf1.setBounds(new Rectangle(160, 50, 150, 25));
tf2.setBounds(new Rectangle(160, 100, 150, 25));
tf3.setBounds(new Rectangle(160, 150, 150, 25));
/*ta.setEditable(true);
ta.setText(" ");
ta.setBackground(Color.white);
ta.setBounds(new Rectangle(40, 100, 400, 200));*/
label1.setBounds(new Rectangle(80, 50, 100, 25));
label2.setBounds(new Rectangle(80, 100, 100, 25));
label3.setBounds(new Rectangle(80, 150, 100, 25));
button.setBounds(new Rectangle(80, 250, 100, 25));
add(label1);
add(label2);
add(label3);
add(tf1);
add(tf2);
add(tf3);
add(ta);
add(button);
setVisible(true);
}
public static void main(String[] args) throws IOException {
Task4 t = new Task4("创建文件");
}
}
class FileImplement implements ActionListener{
public void actionPerformed(ActionEvent e) {
String t1 = Task4.tf1.getText();
String t2 = Task4.tf2.getText();
Integer T2 = Integer.parseInt(t2);
if(T2 < 10)
{
T2 = T2 * 1000000000;
long start = System.currentTimeMillis();
try {
RandomAccessFile raf = new RandomAccessFile(t1, "rw");
raf.setLength(T2);
while(raf.getFilePointer() < T2){
raf.writeUTF("0");
}
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
long end = System.currentTimeMillis();
long time = end-start;
String Time = String.valueOf(time);
Task4.tf3.setText(Time);
}
else {
JOptionPane.showMessageDialog(null, "the file is too large,please reinput");
}
}
}
快速创建大文件
最新推荐文章于 2023-01-05 13:06:34 发布