RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java中移动文件代码,java中移动文件代码是什么

怎样使用java编程实现文件的剪切/移动

可以通过BufferedReader

涿州网站建设公司创新互联,涿州网站设计制作,有大型网站制作公司丰富经验。已为涿州超过千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的涿州做网站的公司定做!

流的形式进行流读取,之后通过readLine方法获取到的内容,之后通过if判断来实现在某些特定位置的内容的剪切和移动操作。

举例:

BufferedReader

bre

=

null;

OutputStreamWriter

pw

=

null;//定义一个流

try

{

String

file

=

"D:/test/test.txt";

bre

=

new

BufferedReader(new

FileReader(file));//此时获取到的bre就是整个文件的缓存流

pw

=

new

OutputStreamWriter(new

FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例

while

((str

=

bre.readLine())!=

null)

//

判断最后一行不存在,为空结束循环

{

if(str.indexOf("排除")0){//判断是否需要舍弃

pw.write(str);//将要写入文件的内容,可以多次write

}

}

bre.close();//关闭流

pw.close();//关闭流

解释:以上方法是实现的删除,if中的条件改变下,即可实现其余的功能。

备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

Java怎么移动文件夹里的文件到指定文件

是的,用File类的renameTo方法即可,注意目标文件名一定要合法,否则失败!

/**

* 移动文件到指定目录

*

* @param oldPath

* String 如:c:/fqf.txt

* @param newPath

* String 如:d:/fqf.txt

*/

public static void moveFile(String oldPath, String newPath) {

copyFile(oldPath, newPath);

delFile(oldPath);

}

/**

* 移动文件到指定目录

*

* @param oldPath

* String 如:c:/fqf.txt

* @param newPath

* String 如:d:/fqf.txt

*/

public static void moveFolder(String oldPath, String newPath) {

copyFolder(oldPath, newPath);

delFolder(oldPath);

}

Java中如何进行文件(及文件夹)的新建,移动,删除等?给出代码

Java中进行文件(及文件夹)的操作

新建 File file=new File("文件名");如果是文件夹 需要file.mkDir();

移动 不能移动,只能复制文件

删除 file.delete();

java 中怎样把一个文件从一个包中转移到另外一个包里面

/** * 移动文件到指定目录 * @param oldPath String 如:c:/fqf.txt * @param newPath String 如:d:/fqf.txt */ public static void moveFolder(String oldPath, String newPath) { copyFolder(oldPath, newPath); delFolder(oldPath); } /** * 复制整个文件夹内容 * @param oldPath String 原文件路径 如:c:/fqf * @param newPath String 复制后路径 如:f:/fqf/ff * @return boolean */ public static void copyFolder(String oldPath, String newPath) { try { (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 File a=new File(oldPath); String[] file=a.list(); File temp=null; for (int i = 0; i file.length; i++) { if(oldPath.endsWith(File.separator)){ temp=new File(oldPath+file[i]); } else{ temp=new File(oldPath+File.separator+file[i]); } if(temp.isFile()){ FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString()); byte[] b = new byte[1024 * 5]; int len; while ( (len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if(temp.isDirectory()){//如果是子文件夹 copyFolder(oldPath+"/"+file[i], newPath+"/"+file[i]); } } } catch (Exception e) { System.out.println("复制整个文件夹内容操作出错"); e.printStackTrace(); } } /** * 删除文件夹 * @param filePathAndName String 文件夹路径及名称 如c:/fqf * @param fileContent String * @return boolean */ public static void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); //删除空文件夹 } catch (Exception e) { System.out.println("删除文件夹操作出错"); e.printStackTrace(); } }

java中文件移动问题,file.move

File的renameTo()就是移动。

它会删掉原来下面的文件,然后在你指定的路径创建一个文件。

前提你要保证指定的路径存在,不存在就先创建。


分享名称:java中移动文件代码,java中移动文件代码是什么
URL分享:http://cqwzjz.cn/article/hdpogd.html