Java文件操作大全
1创建文件夹
//import javaio;
File myFolderPath = new File(%%1);
try {
if (!myFolderPathexists()) {
myFolderPathmkdir();
}
}
catch (Exception e) {
Systemoutprintln("新建目录操作出错");
eprintStackTrace();
}
2创建文件
//import javaio;
File myFilePath = new File(%%1);
try {
if (!myFilePathexists()) {
myFilePathcreateNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
myFileprintln(%%2);
resultFileclose();
}
catch (Exception e) {
Systemoutprintln("新建文件操作出错");
eprintStackTrace();
}
3删除文件
//import javaio;
File myDelFile = new File(%%1);
try {
myDelFiledelete();
}
catch (Exception e) {
Systemoutprintln("删除文件操作出错");
eprintStackTrace();
}
4删除文件夹
//import javaio;
File delFolderPath = new File(%%1);
try {
delFolderPathdelete(); //删除空文件夹
}
catch (Exception e) {
Systemoutprintln("删除文件夹操作出错");
eprintStackTrace();
}
5删除一个文件下夹所有的文件夹
//import javaio;
File delfile=new File(%%1);
File[] files=delfilelistFiles();
for(int i=0;i<fileslength;i++){
if(files[i]isDirectory()){
files[i]delete();
}
}
6清空文件夹
//import javaio;
File delfilefolder=new File(%%1);
try {
if (!delfilefolderexists()) {
delfilefolderdelete();
}
delfilefoldermkdir();
}
catch (Exception e) {
Systemoutprintln("清空目录操作出错");
eprintStackTrace();
}
7读取文件
//import javaio;
// 逐行读取数据
FileReader fr = new FileReader(%%1);
BufferedReader br = new BufferedReader(fr);
String %%2 = brreadLine();
while (%%2 != null) {
%%3
%%2 = brreadLine();
}
brclose();
frclose();
8写入文件
//import javaio;
// 将数据写入文件
try {
FileWriter fw = new FileWriter(%%1);
fwwrite(%%2);
fwflush();
fwclose();
} catch (IOException e) {
eprintStackTrace();
}
9写入随机文件
//import javaio;
try {
RandomAcessFile logFile=new RandomAcessFile(%%1,"rw");
long lg=logFilelength();
logFileseek(%%2);
logFilewriteByte(%%3);
}catch(IOException ioe){
Systemoutprintln("无法写入文件:"+ioegetMessage());
}
10读取文件属性
//import javaio;
// 文件属性的取得
File af = new File(%%1);
if (afexists()) {
Systemoutprintln(fgetName() + "的属性如下: 文件长度为:" + flength());
Systemoutprintln(fisFile() "是文件" : "不是文件");
Systemoutprintln(fisDirectory() "是目录" : "不是目录");
Systemoutprintln(fcanRead() "可读取" : "不");
Systemoutprintln(fcanWrite() "是隐藏文件" : "");
Systemoutprintln("文件夹的最后修改日期为:" + new Date(flastModified()));
} else {
Systemoutprintln(fgetName() + "的属性如下:");
Systemoutprintln(fisFile() "是文件" : "不是文件");
Systemoutprintln(fisDirectory() "是目录" : "不是目录");
Systemoutprintln(fcanRead() "可读取" : "不");
Systemoutprintln(fcanWrite() "是隐藏文件" : "");
Systemoutprintln("文件的最后修改日期为:" + new Date(flastModified()));
}
if(fcanRead()){
%%2
}
if(fcanWrite()){
%%3
}
11写入属性
//import javaio;
File filereadonly=new File(%%1);
try {
boolean b=filereadonlysetReadOnly();
}
catch (Exception e) {
Systemoutprintln("拒绝写访问:"+eprintStackTrace());
}
12枚举一个文件夹中的所有文件
//import javaio;
//import javautil;
LinkedList<String> folderList = new LinkedList<String>();
folderListadd(%%1);
while (folderListsize() > 0) {
File file = new File(folderListpeek());
folderListremoveLast();
File[] files = filelistFiles();
ArrayList<File> fileList = new ArrayList<File>();
for (int i = 0; i < fileslength; i++) {
if (files[i]isDirectory()) {
folderListadd(files[i]getPath());
} else {
fileListadd(files[i]);
}
}
for (File f : fileList) {
%%2=fgetAbsoluteFile();
%%3
}
}
13复制文件夹
//import javaio;
//import javautil;
LinkedList<String> folderList = new LinkedList<String>();
folderListadd(%%1);
LinkedList<String> folderList2 = new LinkedList<String>();
folderList2add(%%2+ %%1substring(%%1lastIndexOf("\\")));
while (folderListsize() > 0) {
(new File(folderList2peek()))mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderListpeek());
String[] file = folderslist();
File temp = null;
try {
for (int i = 0; i < filelength; i++) {
if (folderListpeek()endsWith(Fileseparator)) {
temp = new File(folderListpeek() + Fileseparator
+ file[i]);
} else {
temp = new File(folderListpeek() + Fileseparator
+ file[i]);
}
if (tempisFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2peek() + Fileseparator
+ (tempgetName())toString());
byte[] b = new byte[5120];
int len;
while ((len = inputread(b)) != -1) {
outputwrite(b, 0, len);
}
outputflush();
outputclose();
inputclose();
}
if (tempisDirectory()) {// 如果是子文件夹
for (File f : templistFiles()) {
if (fisDirectory()) {
folderListadd(fgetPath());
folderList2add(folderList2peek()
+ Fileseparator + fgetName());
}
}
}
}
} catch (Exception e) {
//Systemoutprintln("复制整个文件夹内容操作出错");
eprintStackTrace();
}
folderListremoveFirst();
folderList2removeFirst();
}
14复制一个文件夹下所有的文件夹到另一个文件夹下
//import javaio;
//import javautil;
File copyfolders=new File(%%1);
File[] copyfoldersList=copyfolderslistFiles();
for(int k=0;k<copyfoldersListlength;k++){
if(copyfoldersList[k]isDirectory()){
ArrayList<String>folderList=new ArrayList<String>();
folderListadd(copyfoldersList[k]getPath());
ArrayList<String>folderList2=new ArrayList<String>();
folderList2add(%%2+"/"+copyfoldersList[k]getName());
for(int j=0;j<folderListlength;j++){
(new File(folderList2get(j)))mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderListget(j));
String[] file=folderslist();
File temp=null;
try {
for (int i = 0; i < filelength; i++) {
if(folderListget(j)endsWith(Fileseparator)){
temp=new File(folderListget(j)+"/"+file[i]);
}
else{
temp=new File(folderListget(j)+"/"+Fileseparator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(tempisFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2get(j) + "/" +
(tempgetName())toString());
byte[] b = new byte[5120];
int len;
while ( (len = inputread(b)) != -1) {
outputwrite(b, 0, len);
}
outputflush();
outputclose();
inputclose();
}
if(tempisDirectory()){//如果是子文件夹
folderListadd(folderListget(j)+"/"+file[i]);
folderList2add(folderList2get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
Systemoutprintln("复制整个文件夹内容操作出错");
eprintStackTrace();
}
}
}
}
15移动文件夹
//import javaio;
//import javautil;
LinkedList<String> folderList = new LinkedList<String>();
folderListadd(%%1);
LinkedList<String> folderList2 = new LinkedList<String>();
folderList2add(%%2 + %%1substring(%%1lastIndexOf("\\")));
while (folderListsize() > 0) {
(new File(folderList2peek()))mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderListpeek());
String[] file = folderslist();
File temp = null;
try {
for (int i = 0; i < filelength; i++) {
if (folderListpeek()endsWith(Fileseparator)) {
temp = new File(folderListpeek() + Fileseparator
+ file[i]);
} else {
temp = new File(folderListpeek() + Fileseparator
+ file[i]);
}
if (tempisFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2peek() + Fileseparator
+ (tempgetName())toString());
byte[] b = new byte[5120];
int len;
while ((len = inputread(b)) != -1) {
outputwrite(b, 0, len);
}
outputflush();
outputclose();
inputclose();
if (!tempdelete())
Systemoutprintln("删除单个文件操作出错!");
}
if (tempisDirectory()) {// 如果是子文件夹
for (File f : templistFiles()) {
if (fisDirectory()) {
folderListadd(fgetPath());
folderList2add(folderList2peek()
+ Fileseparator + fgetName());
}
}
}
}
} catch (Exception e) {
// Systemoutprintln("复制整个文件夹内容操作出错");
eprintStackTrace();
}
folderListremoveFirst();
folderList2removeFirst();
}
File f = new File(%%1);
if (!fdelete()) {
for (File file : flistFiles()) {
if (filelist()length == 0) {
Systemoutprintln(filegetPath());
filedelete();
}
}
}
16移动一个文件夹下所有的文件夹到另一个目录下
//import javaio;
//import javautil;
File movefolders=new File(%%1);
File[] movefoldersList=movefolderslistFiles();
for(int k=0;k<movefoldersListlength;k++){
if(movefoldersList[k]isDirectory()){
ArrayList<String>folderList=new ArrayList<String>();
folderListadd(movefoldersList[k]getPath());
ArrayList<String>folderList2=new ArrayList<String>();
folderList2add(%%2+"/"+movefoldersList[k]getName());
for(int j=0;j<folderListlength;j++){
(new File(folderList2get(j)))mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderListget(j));
String[] file=folderslist();
File temp=null;
try {
for (int i = 0; i < filelength; i++) {
if(folderListget(j)endsWith(Fileseparator)){
temp=new File(folderListget(j)+"/"+file[i]);
}
else{
temp=new File(folderListget(j)+"/"+Fileseparator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(tempisFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2get(j) + "/" +
(tempgetName())toString());
byte[] b = new byte[5120];
int len;
while ( (len = inputread(b)) != -1) {
outputwrite(b, 0, len);
}
outputflush();
outputclose();
inputclose();
tempdelete();
}
if(tempisDirectory()){//如果是子文件夹
folderListadd(folderListget(j)+"/"+file[i]);
folderList2add(folderList2get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
Systemoutprintln("复制整个文件夹内容操作出错");
eprintStackTrace();
}
}
movefoldersList[k]delete();
}
}
17以一个文件夹的框架在另一个目录创建文件夹和空文件
//import javaio;
//import javautil;
boolean b=false;//不创建空文件
ArrayList<String>folderList=new ArrayList<String>();
folderListadd(%%1);
ArrayList<String>folderList2=new ArrayList<String>();
folderList2add(%%2);
for(int j=0;j<folderListlength;j++){
(new File(folderList2get(j)))mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderListget(j));
String[] file=folderslist();
File temp=null;
try {
for (int i = 0; i < filelength; i++) {
if(folderListget(j)endsWith(Fileseparator)){
temp=new File(folderListget(j)+"/"+file[i]);
}
else{
temp=new File(folderListget(j)+"/"+Fileseparator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(tempisFile()){
if (b) tempcreateNewFile();
}
if(tempisDirectory()){//如果是子文件夹
folderListadd(folderListget(j)+"/"+file[i]);
folderList2add(folderList2get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
Systemoutprintln("复制整个文件夹内容操作出错");
eprintStackTrace();
}
}
18复制文件
//import javaio;
int bytesum = 0;
int byteread = 0;
File oldfile = new File(%%1);
try {
if (oldfileexists()) { //文件存在时
FileInputStream inStream = new FileInputStream(oldfile); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(%%2,oldfilegetName()));
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStreamread(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
Systemoutprintln(bytesum);
fswrite(buffer, 0, byteread);
}
inStreamclose();
}
}
catch (Exception e) {
Systemoutprintln("复制单个文件操作出错");
eprintStackTrace();
}
19复制一个文件夹下所有的文件到另一个目录
//import javaio;
File copyfiles=new File(%%1);
File[] files=copyfileslistFiles();
for(int i=0;i<fileslength;i++){
if(!files[i]isDirectory()){
int bytesum = 0;
int byteread = 0;
try {
InputStream inStream = new FileInputStream(files[i]); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(%%2,files[i]getName());
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStreamread(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
Systemoutprintln(bytesum);
fswrite(buffer, 0, byteread);
}
inStreamclose();
} catch (Exception e) {
Systemoutprintln("复制单个文件操作出错");
eprintStackTrace();
}
}
}
20提取扩展名
String %%2=%%1substring(%%1lastIndexOf("")+1);
Coursejava
public class Course {
private String name;//课程名
private String id; //编号
private String selectId; //选修课编号
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public String getId() {
return id;
}
public void setId(String id) {
thisid = id;
}
public String getSelectId() {
return selectId;
}
public void setSelectId(String selectId) {
thisselectId = selectId;
}
}
1、i=0循环后,number=1 price=9 money=19+0=9
i=1循环后,number=2 price=8 money=28+9=25
i=2跳出循环
输出money=25
2、选b
以下是规定r=Mathrandom的范围:
00<=r<10
d<DoubleMax_Value是说d小于double类型数据的最大值
DoubleMAX_VALUE是java的double类里定义的
第一题
1
Face接口定义了counter属性,Test类继承Face接口,但Test类可以不实现Face接口的属性counter,但如果是方法,必须实现。所以Test的counter与Face的counter无关,本例中Test类中counter为静态,所以初始值默认为0,输出++counter-->1
第二题
2
int i=1; //i=1
int j=i++; //j=i=1 ,i++ -->i=2
if语句
(i>++j) //i=2,j先加1,在比较-->2>2-->false
所以if下语句(i += j;)不执行;
if语句用&&链接前后两部分,前者(i>++j)为false,并且是"与"关系,后面的为任何值,if语句都为false,所以if语句(i++ ==j)不执行,即:i=2,j=2
欢迎分享,转载请注明来源:表白网
评论列表(0条)