📄 hanzi.java
字号:
import java.io.*;
public class HanZi{
public static void main(String args[]){
String str = "软件工程是一门非常重要的课程!";
boolean boolObj = false;
try{
//创建一个File对象fileObj
File fileObj = new File("stuIfo.txt");
/*下面的语句首先通过使用getBytes()方法将参数字符串放到一个byte数组中,"8859_1"用于
指定编码方式,使得输入的汉字可以正确的输入到文件中*/
String wrStr = new String(str.getBytes(),"8859_1");
//String wrStr = new String(str.getBytes());
/*如果当前目录下不存在stuIfo.txt文件,就创建该文件*/
if(fileObj.exists()==false)
boolObj = fileObj.createNewFile();
/*创建一个RandomAccessFile对象,用于进行随机访问文件的处理*/
RandomAccessFile ranFile = new RandomAccessFile(fileObj,"rw");
//下面两个语句将文件指针放在文件的结尾处
long len = ranFile.length();
ranFile.seek(len);
/*将字符串wrStr写入文件的结尾处,并且每次写入新的信息,都换行写入*/
ranFile.writeBytes(wrStr+"\r\n");
ranFile.writeBytes("How are you"+"\r\n");
ranFile.seek(0);
String tempStr;
byte buf[] = new byte[50];
ranFile.readFully(buf);
tempStr = new String(buf);
System.out.println(tempStr);
//tempStr = ranFile.readLine();
//System.out.println(tempStr);
ranFile.close();
}catch(UnsupportedEncodingException e){
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -