public String getInputMessage() throws IOException {
System.out.print("請輸入命令:");
byte[] buffer = new byte[1024];
int count = System.in.read(buffer);
// 去除換行符(Windows \r\n 佔 2 字節,Linux \n 佔 1 字節,這裡簡化處理)
char[] ch = new char[count - 2];
for (int i = 0; i < count - 2; i++) ch[i] = (char) buffer[i];
return new String(ch);
}
缺點:不支持中文輸入(需用 InputStreamReader + 指定編碼)。
public void copyFile(String src, String dest) throws IOException {
try (FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest)) {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
}
}
✅ 支持 txt、XML、jpg、doc 等任意格式;✅ try-with-resources 自動關閉資源(JDK 7+)。
public void printStreamDemo() {
try (PrintStream p = new PrintStream(new FileOutputStream("D:/test.txt"))) {
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();
try (FileOutputStream out = new FileOutputStream(file, true)) { // true = 追加模式
for (int i = 0; i < 10000; i++) {
StringBuffer sb = new StringBuffer();
sb.append("這是第").append(i).append("行...");
out.write(sb.toString().getBytes("UTF-8"));
}
}
}
public void renameFile(String path, String oldName, String newName) {
if (oldName.equals(newName)) return;
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 moveFile(String fileName, String oldPath, String newPath, boolean overwrite) {
if (oldPath.equals(newPath)) return;
File src = new File(oldPath, fileName);
File dest = new File(newPath, fileName);
if (dest.exists()) {
if (overwrite) src.renameTo(dest);
else System.out.println("目標目錄已存在:" + fileName);
} else {
src.renameTo(dest);
}
}
public String readByFileInputStream(String path) throws IOException {
File file = new File(path);
if (!file.exists() || file.isDirectory()) throw new FileNotFoundException();
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
int len;
while ((len = fis.read(buf)) != -1) {
sb.append(new String(buf, 0, len, StandardCharsets.UTF_8));
}
return sb.toString();
}
}
public String readByBufferedReader(String path) throws IOException {
File file = new File(path);
if (!file.exists() || file.isDirectory()) throw new FileNotFoundException();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
}
}
public Document readXml(String path) throws DocumentException, IOException {
File file = new File(path);
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
SAXReader reader = new SAXReader();
return reader.read(br);
}
}
// 創建目錄(含多級)
public void createDir(String path) {
new File(path).mkdirs();
}
// 創建文件
public void createFile(String path, String fileName) throws IOException {
File file = new File(path, fileName);
if (!file.exists()) file.createNewFile();
}
// 刪除文件
public void deleteFile(String path, String fileName) {
File file = new File(path, fileName);
if (file.exists() && file.isFile()) file.delete();
}
// 遞歸刪除目錄
public void deleteDir(String path) {
File dir = new File(path);
if (dir.exists()) {
File[] files = dir.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) deleteDir(f.getAbsolutePath());
else f.delete();
}
}
dir.delete();
}
}
FileChannel.transferTo/From (NIO) 或緩衝區 64KB+new File(parent, child) 或 Paths.get(),避免手動拼接分隔符Files 工具類:Files.copy/move/delete/readAllLines/write 更簡潔、支持屬性複製、原子移動