如何在java中将文件写入特定文件夹



我编写了这个程序,它使用robot类和buffereimage类截取屏幕截图。包含屏幕截图的.png文件被保存到.java文件所在的文件夹中。我想要的图像文件写在另一个文件夹称为截图,我创建。我尝试重命名文件,也尝试使用inputstream和outputstream类复制,但结果相同。我怎样才能克服这些呢?我使用windows 8和java 1.8.0

package screenshot;
//Packages imported for the program
import java.io.*;
import java.nio.file.*;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.util.*;
import java.text.*;
//Class logic written beside each statement
public class Screenshot 
{
   public static void main(String[] args) 
   {       
     try                                                                                    
     {                                                //Exception Handling
        SimpleDateFormat df=new SimpleDateFormat("dd-MM-yy H-m'.png'");                   //Setting the date format 
        Date date=new Date();                                                             //Getting the current date and time
        String ref=df.format(date);                                                       //Formatting the current date in the SDF constructor's format
        //String dirname="M:\Java\bin\screenshot\";
        System.out.println(ref);                                                          //Testing whether the current date is getting displayed in the desired format
        Robot robot = new Robot();                                                        //Instanciating the Robot class       
        Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());          //Declaring a rectangle with size of the screen
        BufferedImage bufferedImage = robot.createScreenCapture(captureSize);                        //Capturing the screenshot and save it in Buffered image object
        //File dir = new File(dirname);
        //File filen = new File("M:\Java\bin\screenshot\temp.png");
        File filename = new File(ref);
        ImageIO.write(bufferedImage, "png" , filename); //writing the image to a file called filename
     }
    catch(Exception e) 
    {
       System.err.println(e);  //Exception message display
    }
   }
}

将文件存储到文件夹screenshot,更改变量

String ref=df.format(date);

String ref = "absolute_path_of_folder/" + df.format(date); // like "C:/screenshot/" + df.format(date);

你试过吗?注意,我对File使用了两个参数的构造函数,它接受表示目录的File和该目录中的文件的名称。我取消了你描述保存到哪个目录的注释,并使用了那个目录。

    SimpleDateFormat df=new SimpleDateFormat("dd-MM-yy H-m'.png'");                   //Setting the date format 
    Date date=new Date();                                                             //Getting the current date and time
    String ref=df.format(date);                                                       //Formatting the current date in the SDF constructor's format
    String dirname="M:\Java\bin\screenshot\";
    System.out.println(ref);                                                          //Testing whether the current date is getting displayed in the desired format
    Robot robot = new Robot();                                                        //Instanciating the Robot class       
    Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());          //Declaring a rectangle with size of the screen
    BufferedImage bufferedImage = robot.createScreenCapture(captureSize);                        //Capturing the screenshot and save it in Buffered image object
    File dir = new File(dirname);
    File filen = new File("M:\Java\bin\screenshot\temp.png");
    File filename = new File(filen, ref);
    ImageIO.write(bufferedImage, "png" , filename); //writing the image to a file called filename

包名与目标文件夹名相同。更改包名或文件夹名,列出的每个方法都可以正常工作

相关内容

  • 没有找到相关文章

最新更新