1.. x:/a/b/c 创建一个文件 1.doc, 键盘输入两个字符串, 写入1.doc中, 两字符串在文件中是两行,
你从1.doc中全部读出来,把每一个字符串都颠倒 写入2.doc中, 从2.doc 在读出来打印。
代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Demo05 {
public static final String path = "G:/a/b/c";
public static final String path1 = "G:/a/b/c/1.doc";
public static final String path2 = "G:/a/b/c/2.doc";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入两个字符串");
String str = sc.next();
String str1 = sc.next();
CreatDir(path);
String contant = str + "\r\n" + str1;
FileOutputStream fos = new FileOutputStream(path1);
Write(fos, contant);
FileInputStream fis = new FileInputStream(path1);
String contant1 = Read(fis);
String[] ss = contant1.split("\n");
String line1 = ss[0];
String line2 = ss[1];
System.out.println("第一个字符串: " + line1);
System.out.println("第二个字符串: " + line2);
System.out.println("第一个字符串颠倒后: " + DianDao(line1));
System.out.println("第二个字符串颠倒后: " + DianDao(line2));
FileOutputStream fos1 = new FileOutputStream(path2,true);
String ss1 = line1+"\r\n"+line2;
Write(fos1, ss1);
}
public static String Read(FileInputStream fis) throws IOException {
byte[] b = new byte[1024];
int len;
String contant1 = "";
while ((len = fis.read(b)) != -1) {
contant1 += new String(b, 0, len);
}
System.out.println(contant1);
return contant1;
}
public static void CreatDir(String path) {
new File(path).mkdirs();
}
public static void CreatFile(String path) {
try {
new File(path).createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void Write(FileOutputStream fos, String contant) {
try {
fos.write(contant.getBytes());
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {