public String getInputMessage() throws IOException {
System.out.println("請輸入您的命令:");
byte[] buffer = new byte[1024];
int count = System.in.read(buffer);
char[] ch = new char[count - 2]; // 去除結束符
for (int i = 0; i < count - 2; i++) ch[i] = (char) buffer[i];
return new String(ch);
}
不支持中文輸入,需改進。
public void copyFile(String src, String dest) throws IOException {
FileInputStream in = new FileInputStream(src);
File file = new File(dest);
if (!file.exists()) file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int c; byte[] buffer = new byte[1024];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) out.write(buffer[i]);
}
in.close(); out.close();
}
public void PrintStreamDemo() {
try {
FileOutputStream out = new FileOutputStream("D:/test.txt");
PrintStream p = new PrintStream(out);
for (int i = 0; i < 10; i++) p.println("This is " + i + " line");
} catch (FileNotFoundException e) { e.printStackTrace(); }
}
public void StringBufferDemo() throws IOException {
File file = new File("/root/sms.log");
if (!file.exists()) file.createNewFile();
FileOutputStream out = new FileOutputStream(file, true); // 追加模式
for (int i = 0; i < 10000; i++) {
StringBuffer sb = new StringBuffer();
sb.append("這是第" + i + "行:...");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}
public void renameFile(String path, String oldname, String newname) {
if (!oldname.equals(newname)) {
File oldfile = new File(path + "/" + oldname);
File newfile = new File(path + "/" + newname);
if (newfile.exists()) System.out.println(newname + "已經存在!");
else oldfile.renameTo(newfile);
}
}
public void changeDirectory(String filename, String oldpath, String newpath, boolean cover) {
if (!oldpath.equals(newpath)) {
File oldfile = new File(oldpath + "/" + filename);
File newfile = new File(newpath + "/" + filename);
if (newfile.exists()) {
if (cover) oldfile.renameTo(newfile);
else System.out.println("在新目錄下已經存在:" + filename);
} else oldfile.renameTo(newfile);
}
}
public String FileInputStreamDemo(String path) throws IOException {
File file = new File(path);
if (!file.exists() || file.isDirectory()) throw new FileNotFoundException();
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024];
StringBuffer sb = new StringBuffer();
while ((fis.read(buf)) != -1) {
sb.append(new String(buf));
buf = new byte[1024];
}
return sb.toString();
}
public String BufferedReaderDemo(String path) throws IOException {
File file = new File(path);
if (!file.exists() || file.isDirectory()) throw new FileNotFoundException();
BufferedReader br = new BufferedReader(new FileReader(file));
String temp; StringBuffer sb = new StringBuffer();
while ((temp = br.readLine()) != null) sb.append(temp + " ");
return sb.toString();
}
public Document readXml(String path) throws DocumentException, IOException {
File file = new File(path);
BufferedReader br = new BufferedReader(new FileReader(file));
SAXReader sax = new SAXReader();
Document doc = (Document) sax.read(br);
br.close();
return doc;
}
public void createDir(String path) { new File(path).mkdir(); }
public void createFile(String path, String filename) throws IOException {
File file = new File(path + "/" + filename);
if (!file.exists()) file.createNewFile();
}
public void delFile(String path, String filename) {
File file = new File(path + "/" + filename);
if (file.exists() && file.isFile()) file.delete();
}
public void delDir(String path) {
File dir = new File(path);
if (dir.exists()) {
for (File tmp : dir.listFiles()) {
if (tmp.isDirectory()) delDir(path + "/" + tmp.getName());
else tmp.delete();
}
dir.delete();
}
}