//1.创建文件夹
//import java.io.*;
File myFolderPath = new File(str1);
try {
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
} //2.创建文件
//import java.io.*;
File myFilePath = new File(str1);
try {
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(str2);
resultFile.close();
}
catch (Exception e) {
System.out.println("新建文件操作出错");
e.printStackTrace();
} //3.删除文件
//import java.io.*;
File myDelFile = new File(str1);
try {
myDelFile.delete();
}
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
} //4.删除文件夹
//import java.io.*;
File delFolderPath = new File(str1);
try {
delFolderPath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
} //5.删除一个文件下夹所有的文件夹
//import java.io.*;
File delfile=new File(str1);
File[] files=delfile.listFiles();
for(int i=;i<files.length;i++){
if(files[i].isDirectory()){
files[i].delete();
}
} //6.清空文件夹
//import java.io.*;
File delfilefolder=new File(str1);
try {
if (!delfilefolder.exists()) {
delfilefolder.delete();
}
delfilefolder.mkdir();
}
catch (Exception e) {
System.out.println("清空目录操作出错");
e.printStackTrace();
} //7.读取文件
//import java.io.*;
// 逐行读取数据
FileReader fr = new FileReader(str1);
BufferedReader br = new BufferedReader(fr);
String str2 = br.readLine();
while (str2 != null) {
str3
str2 = br.readLine();
}
br.close();
fr.close(); //8.写入文件
//import java.io.*;
// 将数据写入文件
try {
FileWriter fw = new FileWriter(str1);
fw.write(str2);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
} //9.写入随机文件
//import java.io.*;
try {
RandomAcessFile logFile=new RandomAcessFile(str1,"rw");
long lg=logFile.length();
logFile.seek(str2);
logFile.writeByte(str3);
}catch(IOException ioe){
System.out.println("无法写入文件:"+ioe.getMessage());
} //10.读取文件属性
//import java.io.*;
// 文件属性的取得
File f = new File(str1);
if (af.exists()) {
System.out.println(f.getName() + "的属性如下: 文件长度为:" + f.length());
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目录" : "不是目录");
System.out.println(f.canRead() ? "可读取" : "不");
System.out.println(f.canWrite() ? "是隐藏文件" : "");
System.out.println("文件夹的最后修改日期为:" + new Date(f.lastModified()));
} else {
System.out.println(f.getName() + "的属性如下:");
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目录" : "不是目录");
System.out.println(f.canRead() ? "可读取" : "不");
System.out.println(f.canWrite() ? "是隐藏文件" : "");
System.out.println("文件的最后修改日期为:" + new Date(f.lastModified()));
}
if(f.canRead()){
str2
}
if(f.canWrite()){
str3
} //11.写入属性
//import java.io.*;
File filereadonly=new File(str1);
try {
boolean b=filereadonly.setReadOnly();
}
catch (Exception e) {
System.out.println("拒绝写访问:"+e.printStackTrace());
} //12.枚举一个文件夹中的所有文件
//import java.io.*;
//import java.util.*;
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(str1);
while (folderList.size() > ) {
File file = new File(folderList.peek());
folderList.removeLast();
File[] files = file.listFiles();
ArrayList<File> fileList = new ArrayList<File>();
for (int i = ; i < files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
str2=f.getAbsoluteFile();
str3
}
} //13.复制文件夹
//import java.io.*;
//import java.util.*;
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(str1);
LinkedList<String> folderList2 = new LinkedList<String>();
folderList2.add(str2+ str1.substring(str1.lastIndexOf("\\")));
while (folderList.size() > ) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = ; i < file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator
+ file[i]);
} else {
temp = new File(folderList.peek() + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator + (temp.getName()).toString());
byte[] b = new byte[];
int len;
while ((len = input.read(b)) != -) {
output.write(b, , len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {// 如果是子文件夹
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek()
+ File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
//System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
} //14.复制一个文件夹下所有的文件夹到另一个文件夹下
//import java.io.*;
//import java.util.*;
File copyfolders=new File(str1);
File[] copyfoldersList=copyfolders.listFiles();
for(int k=;k<copyfoldersList.length;k++){
if(copyfoldersList[k].isDirectory()){
ArrayList<String>folderList=new ArrayList<String>();
folderList.add(copyfoldersList[k].getPath());
ArrayList<String>folderList2=new ArrayList<String>();
folderList2.add(str2+"/"+copyfoldersList[k].getName());
for(int j=;j<folderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = ; i < file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
} else {
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (temp.getName()).toString());
byte[] b = new byte[];
int len;
while ( (len = input.read(b)) != -) {
output.write(b, , len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
}
} //15.移动文件夹
//import java.io.*;
//import java.util.*;
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(str1);
LinkedList<String> folderList2 = new LinkedList<String>();
folderList2.add(str2 + str1.substring(str1.lastIndexOf("\\")));
while (folderList.size() > ) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = ; i < file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator + file[i]);
} else {
temp = new File(folderList.peek() + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator + (temp.getName()).toString());
byte[] b = new byte[];
int len;
while ((len = input.read(b)) != -) {
output.write(b, , len);
}
output.flush();
output.close();
input.close();
if (!temp.delete())
System.out.println("删除单个文件操作出错!");
}
if (temp.isDirectory()) {// 如果是子文件夹
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek() + File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
// System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
}
File f = new File(str1);
if (!f.delete()) {
for (File file : f.listFiles()) {
if (file.list().length == ) {
System.out.println(file.getPath());
file.delete();
}
}
}
//16.移动一个文件夹下所有的文件夹到另一个目录下
//import java.io.*;
//import java.util.*;
File movefolders=new File(str1);
File[] movefoldersList=movefolders.listFiles();
for(int k=;k<movefoldersList.length;k++){
if(movefoldersList[k].isDirectory()){
ArrayList<String>folderList=new ArrayList<String>();
folderList.add(movefoldersList[k].getPath());
ArrayList<String>folderList2=new ArrayList<String>();
folderList2.add(str2+"/"+movefoldersList[k].getName());
for(int j=;j<folderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = ; i < file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (temp.getName()).toString());
byte[] b = new byte[];
int len;
while ( (len = input.read(b)) != -) {
output.write(b, , len);
}
output.flush();
output.close();
input.close();
temp.delete();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
movefoldersList[k].delete();
}
} //17.以一个文件夹的框架在另一个目录创建文件夹和空文件
//import java.io.*;
//import java.util.*;
boolean b=false;//不创建空文件
ArrayList<String>folderList=new ArrayList<String>();
folderList.add(str1);
ArrayList<String>folderList2=new ArrayList<String>();
folderList2.add(str2);
for(int j=;j<folderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = ; i < file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
if (b) temp.createNewFile();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
} //18.复制文件
//import java.io.*;
int bytesum = ;
int byteread = ;
File oldfile = new File(str1);
try {
if (oldfile.exists()) { //文件存在时
FileInputStream inStream = new FileInputStream(oldfile); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(str2,oldfile.getName()));
byte[] buffer = new byte[];
int length;
while ( (byteread = inStream.read(buffer)) != -) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, , byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
} //19.复制一个文件夹下所有的文件到另一个目录
//import java.io.*;
File copyfiles=new File(str1);
File[] files=copyfiles.listFiles();
for(int i=;i<files.length;i++){
if(!files[i].isDirectory()){
int bytesum = ;
int byteread = ;
try {
InputStream inStream = new FileInputStream(files[i]); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(str2,files[i].getName());
byte[] buffer = new byte[];
int length;
while ( (byteread = inStream.read(buffer)) != -) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, , byteread);
}
inStream.close();
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
} //提取扩展名
String str2=str1.substring(str1.lastIndexOf(".")+);

Java文件操作大全的更多相关文章

  1. Java 文件操作大全

    Java 文件操作大全 //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPat ...

  2. 【转】java文件操作大全

    一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{         System.out.println(&qu ...

  3. Java文件操作源码大全

    Java文件操作源码大全 1.创建文件夹 52.创建文件 53.删除文件 54.删除文件夹 65.删除一个文件下夹所有的文件夹 76.清空文件夹 87.读取文件 88.写入文件 99.写入随机文件 9 ...

  4. java日期操作大全

    摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个:  取得指定月份的第一天与取得指定月份的最后一天  http ...

  5. JAVA文件操作类和文件夹的操作代码示例

    JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...

  6. Python文件操作大全

    Python 编程文件操作大全   文件打开模式 打开模式 执行操作 'r' 以只读方式打开文件(默认) 'w' 以写入的方式打开文件,会覆盖已存在的文件 'x' 如果文件已经存在,使用此模式打开将引 ...

  7. java文件操作(普通文件以及配置文件的读写操作)

    转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...

  8. 【整理】C#文件操作大全(SamWang)

    [整理]C#文件操作大全(SamWang) 文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msd ...

  9. 14、Java文件操作stream、File、IO

    1.文件操作涉及到的基本概念 File File类 是文件操作的主要对象中文意义就是 文件 顾名思意 万物皆文件,在计算上看到的所有东西都是文件保存,不管是你的图片.视频.数据库数据等等都是按照基本的 ...

随机推荐

  1. jupyter notebook变量高亮

    首先声明,anaconda安装的时候,一定要勾选“Add Anaconda to my PATH environment variable”! 否则会有一堆麻烦的问题,做了这一步就能自动添加好路径!不 ...

  2. OpenGL 笔记<3> 数据传递 一

    Sending data to a shader using vertex attributes and vertex buffer object 上次我们说到着色器的编译和连接,后面的事情没有做过多 ...

  3. mongodb的yum源配置和安装

    安装前注意: 此教程是通过yum安装的.仅限64位centos系统 安装步骤: 1.创建仓库文件: vi /etc/yum.repos.d/mongodb-org-3.4.repo 然后复制下面配置, ...

  4. phantomjs-prebuilt@2.1.16 install: `node install.js`

    今天运行vue项目安装项目依赖(npm install)的时候,报这个错误: 解决办法: npm -g install phantomjs-prebuilt@2.1.16 --ignore-scrip ...

  5. 提高django model效率的几个小方法

    django的model效率不是很高,特别是在做大量的数据库操作的时候,如果你只用django来开企业站或者外包项目的话,那可以小跳过下,而你恰巧是效率狂或者说是对程序的效率要求比较高的话,那就要注意 ...

  6. python opencv3 FLANN单应性匹配

    git:https://github.com/linyi0604/Computer-Vision 匹配准确率非常高. 单应性指的是图像在投影发生了 畸变后仍然能够有较高的检测和匹配准确率 # codi ...

  7. hdu 3061 最大权闭合子图

    属于模板题吧... #include <cstdio> #include <cstring> #include <vector> #define min(a,b) ...

  8. HDU 5699 货物运输 二分

    货物运输 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5699 Description 公元2222年,l国发生了一场战争. 小Y负责领导工人运输物 ...

  9. Qt 编译boost

    Qt为4.6.2.Boost为1.63.0. 1.安装qt-sdk-win-opensource-2010.02.1.exe. 2.下载boost_1_63_0并解压,如:解压到E盘根目录下. 3.在 ...

  10. 不错的SDL源码分析

     SDL源码分析 1:初始化(SDL_Init()) 2:窗口(SDL_Window) 3:渲染器(SDL_Renderer) 4:纹理(SDL_Texture) 5:更新纹理(SDL_UpdateT ...