文件 reading/writing 正确顺序
File reading/writing proper order
所以我正在开发这个程序,它需要创建一个文件,写入该文件,然后从该文件中读取。但是,该文件并未被写入,而是仍在创建中。首先,这是我的文件中构造函数的声明,用于执行 class(称为 filer):
private static FileWriter fw;
private static FileReader fr;
private static BufferedWriter bw;
private static BufferedReader br;
mainclass的main方法首先执行这个openFile()方法,它在filerclass中。这是 openFile():
public static void openFile() {
try{ //exception handling
file = new File("jibberish.txt");
fw = new FileWriter(file); //dont put filewriter or buffedwriter before these, because already declared and end up
bw = new BufferedWriter(fw); //making null expection thing
fr = new FileReader(file);
br = new BufferedReader(fr);
}catch(IOException ioe){
System.out.println("Trouble reading from the file: " + ioe.getMessage());
}
}
现在在 main 方法中,我设置了一个面板,该面板在另一个连接的 class 上都可以正常工作。有一个 jTextField 接受用户输入的内容,并通过这个 class 运行它,这是我似乎无法实际写入文件的地方。
public static void addStuff(String toAdd){
String line = "";
try {
bw.write(toAdd);
//bw.newLine();
System.out.println(toAdd + " added");
//line = br.readLine();
while((line = br.readLine()) != null) {
System.out.println(line + "r");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error at addstuff");
}
}
所以我需要程序做的,它现在没有做的,实际上是写入文件并能够读取它。提前致谢!
我不确定真正的问题是什么,因为我只是做了你刚才做的并且对我有用:
这里是完整的 class 代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import org.apache.poi.xslf.model.geom.AddSubtractExpression;
public class a {
private static FileWriter fw;
private static FileReader fr;
private static BufferedWriter bw;
private static BufferedReader br;
static File file;
public static void openFile() {
try{ //exception handling
file = new File("jibberish.txt");
fw = new FileWriter(file); //dont put filewriter or buffedwriter before these, because already declared and end up
bw = new BufferedWriter(fw); //making null expection thing
fr = new FileReader(file);
br = new BufferedReader(fr);
}catch(Exception ioe){
System.out.println("Trouble reading from the file: " + ioe.getMessage());
}
}
public static void addStuff(String toAdd){
String line = "";
try {
bw.write(toAdd);
//bw.newLine();
System.out.println(toAdd + " added");
//line = br.readLine();
while((line = br.readLine()) != null) {
System.out.println(line + "r");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error at addstuff");
}
}
public static void main(String... args){
openFile();
int i=0;
while(true)
addStuff("hello" + i++);
}
}
一定要加上
bw.flush();
在写入后刷新缓冲区并将数据放入文件以便能够读回您的代码。
所以我正在开发这个程序,它需要创建一个文件,写入该文件,然后从该文件中读取。但是,该文件并未被写入,而是仍在创建中。首先,这是我的文件中构造函数的声明,用于执行 class(称为 filer):
private static FileWriter fw;
private static FileReader fr;
private static BufferedWriter bw;
private static BufferedReader br;
mainclass的main方法首先执行这个openFile()方法,它在filerclass中。这是 openFile():
public static void openFile() {
try{ //exception handling
file = new File("jibberish.txt");
fw = new FileWriter(file); //dont put filewriter or buffedwriter before these, because already declared and end up
bw = new BufferedWriter(fw); //making null expection thing
fr = new FileReader(file);
br = new BufferedReader(fr);
}catch(IOException ioe){
System.out.println("Trouble reading from the file: " + ioe.getMessage());
}
}
现在在 main 方法中,我设置了一个面板,该面板在另一个连接的 class 上都可以正常工作。有一个 jTextField 接受用户输入的内容,并通过这个 class 运行它,这是我似乎无法实际写入文件的地方。
public static void addStuff(String toAdd){
String line = "";
try {
bw.write(toAdd);
//bw.newLine();
System.out.println(toAdd + " added");
//line = br.readLine();
while((line = br.readLine()) != null) {
System.out.println(line + "r");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error at addstuff");
}
}
所以我需要程序做的,它现在没有做的,实际上是写入文件并能够读取它。提前致谢!
我不确定真正的问题是什么,因为我只是做了你刚才做的并且对我有用:
这里是完整的 class 代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import org.apache.poi.xslf.model.geom.AddSubtractExpression;
public class a {
private static FileWriter fw;
private static FileReader fr;
private static BufferedWriter bw;
private static BufferedReader br;
static File file;
public static void openFile() {
try{ //exception handling
file = new File("jibberish.txt");
fw = new FileWriter(file); //dont put filewriter or buffedwriter before these, because already declared and end up
bw = new BufferedWriter(fw); //making null expection thing
fr = new FileReader(file);
br = new BufferedReader(fr);
}catch(Exception ioe){
System.out.println("Trouble reading from the file: " + ioe.getMessage());
}
}
public static void addStuff(String toAdd){
String line = "";
try {
bw.write(toAdd);
//bw.newLine();
System.out.println(toAdd + " added");
//line = br.readLine();
while((line = br.readLine()) != null) {
System.out.println(line + "r");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error at addstuff");
}
}
public static void main(String... args){
openFile();
int i=0;
while(true)
addStuff("hello" + i++);
}
}
一定要加上
bw.flush();
在写入后刷新缓冲区并将数据放入文件以便能够读回您的代码。