文件编程题
1.编写一个程序,将 a.txt 文件中的单词与 b.txt 文件中的单词交替合并到c.txt 文件中,a.txt 文件中的单词用回车符分隔,b.txt 文件中用回车或空格进行分隔
public static void main(String args[]){
BufferedReader a = new BufferedReader(
new InputStreamReader(
new FileInputStream("a.txt")));
BufferedReader b = new BufferedReader(
new InputStreamReader(
new FileInputStream("b.txt")));
PrintWriter c = new PrintWriter(new OutputStreamWriter(new FileOutputStream("c.txt", true)), true);
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
String s = null;
while ((s = a.readLine()) != null) {
/*\r 回车符*/
String array[] = s.split("\r");
for (String word : array) {
list1.add(word);
}
}
String s1 = null;
while ((s1 = b.readLine()) != null) {
String array[] = s1.split(" ");
for (String word : array) {
list2.add(word);
}
}
if (list1.size() >= list2.size()) {
for (int i = 0; i < list1.size(); i++) {
c.write(list1.get(i) + " ");
if (i < list2.size()) {
c.write(list2.get(i) + " ");
}
}
} else {
for (int i = 0; i < list2.size(); i++) {
c.write(list2.get(i) + " ");
if (i < list1.size()) {
c.write(list1.get(i) + " ");
}
}
}
c.flush();
c.close();
b.close();
a.close();
}
2.bmp格式的照⽚进⾏反⾊处理即传⼊⼀张bmp格式的照⽚,对这张照⽚的颜⾊进⾏相反⾊调的处理
提示:图⽚也是⽂件,最根本形式的都是⽤byte数组存储
bmp格式的照⽚前54个byte的数据为照⽚的头⽂件信息,不可更改
其余每个byte数据,只要 0xff - 原来的数字,便可起到反⾊效果
@param src 原来照⽚
@param target 反⾊之后的照⽚
注意:反⾊之后的照⽚必须是传⼊的target⽂件的位置
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
src));
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(target));
int header=0;
int len = 0;
while ((len = in.read()) != -1) {
if (header > 54) {
out.write(0xff -len);
} else {
out.write(len);
}
header++;
}
in.close();
out.close();
}