-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseRandom.java
More file actions
50 lines (49 loc) · 1.02 KB
/
Copy pathUseRandom.java
File metadata and controls
50 lines (49 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.*;
public class UseRandom
{
public static void main(String[] args)
{
try
{
// 构建随机访问文件对象
RandomAccessFile f = new RandomAccessFile("Hello.tx", "rw");
// 得到文件指针和长度
long flag = 0;
long len=f.length();
// 字符处理后输出
while (flag < len)
{
String s = f.readLine();
System.out.println(parseChinese(s));
flag = f.getFilePointer();
}
// 末尾写入字符
f.writeChar('O');
f.writeChar('K');
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
// 解决中文转换问题
public static String parseChinese(String inStr)
{
String s = null;
byte temp[];
if (inStr == null)
{
return new String("");
}
try
{
temp = inStr.getBytes("iso-8859-1");
s = new String(temp);
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return s;
}
}