第一题:ABC
第二题
class TestMyFile{
public static void main(String args[])throws Exception{
File file;
//创建一个File对象表示当前目录下的“hello.txt”文件
File file = new File(“hello.txt”);
//判断该文件是否存在
//如果该文件存在,则输出该文件的完整路径
If(file.exists()){
System.out.pringln(file.getAbsolutePath());
}
}
}
第十题
FileWriter :用于写入字符文件的方便类。该类的构造函数假定默认字符编码和默认字节缓冲区大小是可以接受的。指定这些值在一个自己构建一个outputstreamwriter。
PrintWtiter:将对象的格式化的表示形式打印到文本输出流中。
OutputStreamWriter:一个outputstreamwriter是桥从字符流的字节流:将字符串编码为字节,使用指定的 charset。字符集,它使用可指定名称或可给予明确,或平台的默认字符集可以接受。
第十一题
字节流:2、4、5
字符流:1、3
第十二题
ObjectInputStream --> 读对象
ObjectOutputStream --> 写对象
BufferInputStream --> 字节流、缓冲功能
BufferOutputStream --> 字节流、缓冲功能
DataInputStream --> 字节流、读八种基本类型
DataOutputStream --> 字节流、写八种基本类型
PrintWriter --> 字符流、写字符串并换行
PrintStream --> 字节流
BufferedReader --> 字符流、缓冲功能、读入一行文本
第十四题
十七题
import java.io.;
import java.util.;
public class Test Questuon17 {
public static void main(String args[]) throws Exception {
String str[] = {“意大利”, “巴西”, “法国”, “巴西”};
write(str);
read();
static void write(String[] str) throws IOException {
int year = 2006;
for (int i = 0; i < str.length; i++) {
str[i] = str[i] + “/” + year;
year = year - 4;
}
BufferedWriter bw = new BufferedWriter(new FileWriter(“worldcup.txt”));
for (int i = 0; i < str.length; i++) {
bw.write(str[i]);
bw.newLine();
}
bw.close();
}
static void read() throws IOException {
Scanner a = new Scanner(System.in);
String s = a.next();
BufferedReader br = new BufferedReader(new FileReader("worldcup.txt"));
boolean flag = false;
String str = null;
String strarr[] = new String[2];
while ((str = br.readLine()) != null) {
strarr = str.split("/", 2);
if (strarr[1].equals(s)) {
flag = true;
System.out.println(strarr[0]);
}
}
if (!flag) {
System.out.println("没有举办过世界杯");
}
br.close();
}
十八题
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class TestQuestion18{
@SuppressWarnings(“rawtypes”)
public static void main(String[] args) {
Long lon = 100000L;
File file = new File(“tt.dat”);
if (file.exists()) {
try {
DataInputStream in = new DataInputStream(new FileInputStream(
“tt.dat”));
lon = in.readLong();
in.close();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
List list = new ArrayList();
list.add(new Account(10.00, “1234”));
list.add(new Account(15.00, “5678”));
list.add(new Account(0, “1010”));
TreeMap<Long, Account> acc = new TreeMap<Long, Account>();
for (int i = 0; i < list.size(); i++) {
acc.put((long) (lon + i), (Account) list.get(i));
}
int i = 0;
for (Long mm : acc.keySet()) {
Account ff = (Account) list.get(i);
i++;
ff.setId(mm);
}
for (Object ss : list) {
System.out.println(((Account) ss).getId() + “:”
+ ((Account) ss).getBalance());
}
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(
“tt.dat”));
out.writeLong((Long) (lon + list.size()));
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}