java 记事本代码注释
publicvoidreadFile(StringfileName){try{Filefile=newFile(fileName);FileReaderreadIn=ne...
public void readFile(String fileName){
try{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content, charsRead, size - charsRead);
readIn.close();
textArea.setText(new String(content, 0, charsRead));
}
catch(IOException e){
System.out.println("Error opening file");
}
}
public void writeFile(String fileName){
try{
File file = new File (fileName);
FileWriter writeOut = new FileWriter(file);
writeOut.write(textArea.getText());
writeOut.close();
}
catch(IOException e){
System.out.println("Error writing file");
}
}
public static void main(String[] args){
Frame frame = new EditorFrame();
frame.show();
}
} 展开
try{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content, charsRead, size - charsRead);
readIn.close();
textArea.setText(new String(content, 0, charsRead));
}
catch(IOException e){
System.out.println("Error opening file");
}
}
public void writeFile(String fileName){
try{
File file = new File (fileName);
FileWriter writeOut = new FileWriter(file);
writeOut.write(textArea.getText());
writeOut.close();
}
catch(IOException e){
System.out.println("Error writing file");
}
}
public static void main(String[] args){
Frame frame = new EditorFrame();
frame.show();
}
} 展开
1个回答
展开全部
public void readFile(String fileName){
try{
File file = new File(fileName); //创建一个文件对象
FileReader readIn = new FileReader(file); //创建一个文件读取对象
int size = (int)file.length(); //获得文件的长度
int charsRead = 0;
char[] content = new char[size]; //创建一个字符数组
while(readIn.ready()) //当读取不为空时执行将读取的内容存储到数组conten内
charsRead += readIn.read(content, charsRead, size - charsRead);
readIn.close();
textArea.setText(new String(content, 0, charsRead)); //将数组的内容全部显示到textArea内
}
catch(IOException e){ //扑获异常
System.out.println("Error opening file");
}
}
public void writeFile(String fileName){
try{
File file = new File (fileName); //创建一个文件类
FileWriter writeOut = new FileWriter(file); //创建一个文件写出类
writeOut.write(textArea.getText()); //将textArea的内容全部放到writeout内
writeOut.close(); //关闭writeout之前,会自动将其中的内容保存到file内
}
catch(IOException e){ 扑获异常
System.out.println("Error writing file");
}
}
public static void main(String[] args){ //这是主方法
Frame frame = new EditorFrame(); //创建一个Frame的类
frame.show(); //调用freame的show()方法
}
}
try{
File file = new File(fileName); //创建一个文件对象
FileReader readIn = new FileReader(file); //创建一个文件读取对象
int size = (int)file.length(); //获得文件的长度
int charsRead = 0;
char[] content = new char[size]; //创建一个字符数组
while(readIn.ready()) //当读取不为空时执行将读取的内容存储到数组conten内
charsRead += readIn.read(content, charsRead, size - charsRead);
readIn.close();
textArea.setText(new String(content, 0, charsRead)); //将数组的内容全部显示到textArea内
}
catch(IOException e){ //扑获异常
System.out.println("Error opening file");
}
}
public void writeFile(String fileName){
try{
File file = new File (fileName); //创建一个文件类
FileWriter writeOut = new FileWriter(file); //创建一个文件写出类
writeOut.write(textArea.getText()); //将textArea的内容全部放到writeout内
writeOut.close(); //关闭writeout之前,会自动将其中的内容保存到file内
}
catch(IOException e){ 扑获异常
System.out.println("Error writing file");
}
}
public static void main(String[] args){ //这是主方法
Frame frame = new EditorFrame(); //创建一个Frame的类
frame.show(); //调用freame的show()方法
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询