所以我要做的就是将clientName, date, hour
写入fileWriter.txt文档。我写的是Random Access Memory File
。目前,如果我输入正确的值,它将以错误的格式输出!
Client Name = "James", Date="1", Hour="1"
出于某种原因,这将输出以下行 - 00,00, , null01,James ,1 ,
他们根据他们输入的date
作为文件的作者。
另外,有没有办法可以写入文件,然后删除GUI值,以便输入新值?
##Correct Imports Here
public class CreateRandomDataFile extends JFrame implements ActionListener {
private static JButton submit;
private static JLabel output;
private static JLabel bankDetails;
private static JTextField txtClient = new JTextField("", 20);
private static JTextField txtDate = new JTextField("",6);
private static JTextField txtHour = new JTextField("",6);
private static JLabel lblClient = new JLabel("Client");
private static JLabel lblDate = new JLabel("Date");
private static JLabel lblHour = new JLabel("Hour");
private static Path file = Paths.get("fileWriter.txt");
private static String s = "00, , "
+ System.getProperty("line.seperator");
private static FileChannel fc = null;
private static int RECSIZE = s.length();
private String client = "";
private int date;
private int hour;
public CreateRandomDataFile(){
super("Create Data File");
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
output = new JLabel();
submit = new JButton("Submit");
bankDetails = new JLabel();
setLayout(new FlowLayout());
add(lblClient);
add(txtClient);
txtClient.addActionListener(this);
add(lblDate);
add(txtDate);
txtDate.addActionListener(this);
add(lblHour);
add(txtHour);
txtHour.addActionListener(this);
add(submit);
submit.addActionListener(this);
add(output);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
int counter = 0;
if(source == submit){
client = txtClient.getText();
//int newDate = Integer.parseInt(txtDate.getText());
date = Integer.parseInt(txtDate.getText());
hour = Integer.parseInt(txtHour.getText());
try{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
// OutputStream outStream = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
// BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream));
String as=date + "";
for(int i=as.length();i<2;i++){
//s="";
//s+="0";
s+="0" + date;
}
s+=","+client;
for(int i=client.length();i<10;i++){
s+=" ";
}
s+= "," + hour;
String hourLen = hour + "";
for(int i=hourLen.length();i<2;i++){
s += " ";
}
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
fc.position(date * RECSIZE);
fc.write(buffer);
// writer.write(s, 0, s.length());
// writer.newLine();
fc.close();
}
catch(Exception eStream){
System.out.println("Incorrect");
}
}
}
public static void main(String[] args){
CreateRandomDataFile writer = new CreateRandomDataFile();
writer.setVisible(true);
}
}
public class createRandomNullFile {
public static void main(String[] args){
//Limit of student records allowed
final int NO_STUDENTS = 100;
//Create the file
Path file = Paths.get("fileWriter.txt");
//Default value for every record in the Random Access file (Students.txt)
String s = "00, , "
+ System.getProperty("line.separator"); // After each record goto new line
//Array - Get contents of string and add to data array
byte[] data = s.getBytes();
try{
OutputStream output = new BufferedOutputStream(
Files.newOutputStream(file, CREATE));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output));
for(int i=0;i< NO_STUDENTS;i++){
bw.write(s, 0, s.length());
}
bw.close();
JOptionPane.showMessageDialog(null, "Empty file created");
}
catch(IOException ex){
System.out.println("Error connecting or writing to file");
}
}
答案 0 :(得分:0)
您正在写入文件的String初始化在类的顶部
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);//0 for low, 1 higth
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,20); //20seq
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,size*1048576L);//X mb *1024*1024
startActivityForResult(intent, VIDEO_CAMERA_SELECT);
这是&#34; 00,00,null&#34;字符串的一部分来自(null是由于不正确的属性名称;它应该是line.separator),您应该在使用它之前清除此字符串或创建一个新字符串。
要清除GUI,请在写入文件后将以下内容放在actionPerformed方法中以清除文本字段
private static String s = "00, , "
+ System.getProperty("line.seperator");
答案 1 :(得分:0)
我不清楚你想要获得"00, , "
以外的输出,因为这是你写的唯一字符串。这是您的代码稍作修改:
public class createRandomNullFile {
public static void main(String[] args) {
final int NO_STUDENTS = 10;
Path file = Paths.get("fileWriter.txt");
String s = "00, , " + System.lineSeparator();
try {
OutputStream output = new BufferedOutputStream(Files.newOutputStream(file));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output));
for (int i = 0; i < NO_STUDENTS; i++) {
bw.write(s, 0, s.length());
}
bw.close();
} catch (IOException ex) {
System.out.println("Error connecting or writing to file");
}
}
}
创建一个包含内容的文本文件:
00, ,
00, ,
00, ,
00, ,
00, ,
00, ,
00, ,
00, ,
00, ,
00, ,