public static boolean createFile(String filePath){
boolean result = false;
File file = new File(filePath);
if(!file.exists()){
try {
result = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static boolean createDirectory(String directory){
boolean result = false;
File file = new File(directory);
if(!file.exists()){
result = file.mkdirs();
}
return result;
}
public static boolean deleteFile(String filePath){
boolean result = false;
File file = new File(filePath);
if(file.exists() && file.isFile()){
result = file.delete();
}
return result;
}
public static void deleteDirectory(String filePath){
File file = new File(filePath);
if(!file.exists()){
return;
}
if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
File[] files = file.listFiles();
for (File myfile : files) {
deleteDirectory(filePath + "/" + myfile.getName());
}
file.delete();
}
}
public static String readFileByBytes(String filePath){
File file = new File(filePath);
if(!file.exists() || !file.isFile()){
return null;
}
StringBuffer content = new StringBuffer();
try {
byte[] temp = new byte[1024];
FileInputStream fileInputStream = new FileInputStream(file);
while(fileInputStream.read(temp) != -1){
content.append(new String(temp));
temp = new byte[1024];
}
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
public static String readFileByChars(String filePath){
File file = new File(filePath);
if(!file.exists() || !file.isFile()){
return null;
}
StringBuffer content = new StringBuffer();
try {
char[] temp = new char[1024];
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
while(inputStreamReader.read(temp) != -1){
content.append(new String(temp));
temp = new char[1024];
}
fileInputStream.close();
inputStreamReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
public static List<String> readFileByLines(String filePath){
File file = new File(filePath);
if(!file.exists() || !file.isFile()){
return null;
}
List<String> content = new ArrayList<String>();
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
BufferedReader reader = new BufferedReader(inputStreamReader);
String lineContent = "";
while ((lineContent = reader.readLine()) != null) {
content.add(lineContent);
System.out.println(lineContent);
}
fileInputStream.close();
inputStreamReader.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static void writeFileByFileOutputStream(String filePath, String content) throws IOException{
File file = new File(filePath);
synchronized (file) {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(content.getBytes("GBK"));
fos.close();
}
}
public static void writeFileByBufferedOutputStream(String filePath, String content) throws IOException{
File file = new File(filePath);
synchronized (file) {
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));
fos.write(content.getBytes("GBK"));
fos.flush();
fos.close();
}
}
public static void writeFileByFileWriter(String filePath, String content) throws IOException{
File file = new File(filePath);
synchronized (file) {
FileWriter fw = new FileWriter(filePath);
fw.write(content);
fw.close();
}
}
import java.io.File;
public class CreateDir {
public static void main(String args[]) {
String dirname = "/tmp/user/java/bin";
File d = new File(dirname);
// Create directory now.
d.mkdirs();
}
}
import java.io.File;
public class ReadDir {
public static void main(String[] args) {
File file = null;
String[] paths;
try{
// create new file object
file = new File("/tmp");
// array of files and directory
paths = file.list();
// for each name in the path array
for(String path:paths)
{
// prints filename and directory name
System.out.println(path);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
test1.txt test2.txt ReadDir.java ReadDir.class
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有