问题描述如下:有一个文件A.csv,
内容如下:Q123,"""S,""",F
A123,Jonathan Ackerman,A
B234,Grady O'Neil,B
C456,"Susan, Peter and Dave",C
D789,"Amelia ""meals"" Maurice",E
X234,"Peter ""peg leg"", Jimmy & Samantha ""Sam""",G......................
......................
A的大小大约有几十m。
我现在想在A的最前面一行加上字段名
理想中的内容:
1,2,3,4
Q123,"""S,""",F
A123,Jonathan Ackerman,A
B234,Grady O'Neil,B
C456,"Susan, Peter and Dave",C
D789,"Amelia ""meals"" Maurice",E
X234,"Peter ""peg leg"", Jimmy & Samantha ""Sam""",G
...................
...................大家看明白了,就是想把“1,2,3,4”加入到最上面一行。大家给你高招。小弟利马送100分。不赞成去读a文件,因为他太大了。
内容如下:Q123,"""S,""",F
A123,Jonathan Ackerman,A
B234,Grady O'Neil,B
C456,"Susan, Peter and Dave",C
D789,"Amelia ""meals"" Maurice",E
X234,"Peter ""peg leg"", Jimmy & Samantha ""Sam""",G......................
......................
A的大小大约有几十m。
我现在想在A的最前面一行加上字段名
理想中的内容:
1,2,3,4
Q123,"""S,""",F
A123,Jonathan Ackerman,A
B234,Grady O'Neil,B
C456,"Susan, Peter and Dave",C
D789,"Amelia ""meals"" Maurice",E
X234,"Peter ""peg leg"", Jimmy & Samantha ""Sam""",G
...................
...................大家看明白了,就是想把“1,2,3,4”加入到最上面一行。大家给你高招。小弟利马送100分。不赞成去读a文件,因为他太大了。
1.copy b + a c /Y
2.del a
3.rename c a就可以了。
然后seek到文件尾把这个记录加上就可以了。当然你先要把第一条记录读出来。最XP的方法就是我说上边说的:用写字板
import java.io.RandomAccessFile;public class Test2 { public static void main(String[] args) { try {
RandomAccessFile a = new RandomAccessFile("b","rw");
a.writeChars("1,2,3,4\r\n");
a.close();
a = new RandomAccessFile("a","rw");
a.writeChars("TEST,TEST\r\n");
a.close();
String[] command = {"cmd.exe", "/c", "copy b + a c /Y"};
Runtime.getRuntime().exec(command);
System.out.println("Finished..Copy");
String[] command2 = {"cmd.exe", "/c", "del a"};
Runtime.getRuntime().exec(command2);
System.out.println("Finished..DELETE");
String[] command3 = {"cmd.exe", "/c", "del b"};
Runtime.getRuntime().exec(command3);
System.out.println("Finished..DELETE2");
String[] command4 = {"cmd.exe", "/c", "ren c a"};
Runtime.getRuntime().exec(command4);
System.out.println("Finished..RenName");
} catch (IOException e) {
e.printStackTrace();
}
}
}
WIN2000 Pro,
ECLIPSE2.1.2,
JDK1.4.2_08测试通过.LiNUX的话,需要改成相关的命令.
类似下面.String [] command = {"/bin/sh", "-c", "cp file1 file2"};
Runtime.getRuntime().exec(command);
如果列名比第一行记录的字节数多 嘿嘿 。这个方法是行不通的。还是试试copy吧
如果列名比第一行记录的字节数多 嘿嘿 。这个方法是行不通的。还是试试copy吧
I know that is a god damn answer, however, if you set out to employ Copy solution, so why not take FileWriter or PrintWriter or so on classes like that?Because first I said I thought u want the efficiency, huh?
But you know in the windows OS itself, it employ CopyFileEx function to handle the copy enquiry. Besides, Copy, this action is a read & write process. so its efficiency is totally the same as FileWriter which is based on the Byte.I slao said already the most XP solution is employing things like Notepad coz' it possess the equal efficiency as the Copy, u might deem it is retard coz' it lost the time on GUI. so you know. Regards
Process proces = Runtime.getRuntime().exec("cmd.exe /c start copy d:\\test.csv /b +d:\\sample.csv /b d:\\c.csv /Y/b");打开了dos窗口 郁闷阿? 有啥办法关掉吗?如果我去了 start 那么就不执行,如果去了 /c 也不拷贝。晕阿 救命