-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
024e3d4
commit d69c3ed
Showing
3 changed files
with
33 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,23 @@ | ||
package utils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.*; | ||
|
||
public class FileUtils { | ||
public static void deleteDir(File dir) { | ||
File[] filelist = dir.listFiles(); | ||
for (File file : filelist) { | ||
if (file.isFile()) { | ||
file.delete(); | ||
} else { | ||
deleteDir(file); | ||
} | ||
} | ||
dir.delete(); | ||
} | ||
|
||
public static void copy(File origin, File newfile) throws FileNotFoundException, IOException { | ||
if (!newfile.getParentFile().exists()) { | ||
newfile.getParentFile().mkdirs(); | ||
} | ||
FileInputStream fis = new FileInputStream(origin); | ||
FileOutputStream fos = new FileOutputStream(newfile); | ||
byte[] buf = new byte[2048]; | ||
int read; | ||
while ((read = fis.read(buf)) != -1) { | ||
fos.write(buf, 0, read); | ||
} | ||
fis.close(); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFile(String fileName, String contentStr, String charset) throws FileNotFoundException, IOException { | ||
byte[] content = contentStr.getBytes(charset); | ||
FileOutputStream fos = new FileOutputStream(fileName); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFile(File file, String contentStr, String charset) throws FileNotFoundException, IOException { | ||
byte[] content = contentStr.getBytes(charset); | ||
FileOutputStream fos = new FileOutputStream(file); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFileWithParent(String fileName, String contentStr, String charset) throws FileNotFoundException, IOException { | ||
File file = new File(fileName); | ||
File parent = file.getParentFile(); | ||
if (!parent.exists()) { | ||
parent.mkdirs(); | ||
} | ||
byte[] content = contentStr.getBytes(charset); | ||
FileOutputStream fos = new FileOutputStream(file); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFileWithParent(File file, String contentStr, String charset) throws FileNotFoundException, IOException { | ||
File parent = file.getParentFile(); | ||
if (!parent.exists()) { | ||
parent.mkdirs(); | ||
} | ||
byte[] content = contentStr.getBytes(charset); | ||
FileOutputStream fos = new FileOutputStream(file); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFile(String fileName, byte[] content) throws FileNotFoundException, IOException { | ||
FileOutputStream fos = new FileOutputStream(fileName); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFile(File file, byte[] content) throws FileNotFoundException, IOException { | ||
FileOutputStream fos = new FileOutputStream(file); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFileWithParent(String fileName, byte[] content) throws FileNotFoundException, IOException { | ||
File file = new File(fileName); | ||
File parent = file.getParentFile(); | ||
if (!parent.exists()) { | ||
parent.mkdirs(); | ||
public static void writeFileAppend(String fileName, String content) { | ||
try { | ||
// 打开一个随机访问文件流,按读写方式 | ||
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); | ||
// 文件长度,字节数 | ||
long fileLength = randomFile.length(); | ||
//将写文件指针移到文件尾 | ||
randomFile.seek(fileLength); | ||
randomFile.writeBytes(content); | ||
randomFile.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
FileOutputStream fos = new FileOutputStream(file); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static void writeFileWithParent(File file, byte[] content) throws FileNotFoundException, IOException { | ||
|
||
File parent = file.getParentFile(); | ||
if (!parent.exists()) { | ||
parent.mkdirs(); | ||
} | ||
FileOutputStream fos = new FileOutputStream(file); | ||
fos.write(content); | ||
fos.close(); | ||
} | ||
|
||
public static byte[] readFile(File file) throws IOException { | ||
FileInputStream fis = new FileInputStream(file); | ||
byte[] buf = new byte[2048]; | ||
int read; | ||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
while ((read = fis.read(buf)) != -1) { | ||
bos.write(buf, 0, read); | ||
} | ||
|
||
fis.close(); | ||
return bos.toByteArray(); | ||
} | ||
|
||
public static byte[] readFile(String fileName) throws IOException { | ||
File file = new File(fileName); | ||
return readFile(file); | ||
} | ||
|
||
public static String readFile(File file, String charset) throws Exception { | ||
FileInputStream fis = new FileInputStream(file); | ||
byte[] buf = new byte[2048]; | ||
int read; | ||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
while ((read = fis.read(buf)) != -1) { | ||
bos.write(buf, 0, read); | ||
} | ||
|
||
fis.close(); | ||
return new String(bos.toByteArray(), charset); | ||
} | ||
|
||
public static String readFile(String fileName, String charset) throws Exception { | ||
File file = new File(fileName); | ||
return readFile(file, charset); | ||
} | ||
} |