如何在java中将文件写入特定文件夹?
我用robot类和BuffereImage类做了这个截图程序。包含截图的.png文件将保存到存在.java文件的文件夹中。我希望将图像文件写入我创建的另一个名为screenshot的文件夹中。我尝试重命名文件,也尝试使用inputstream和outputstream类进行复制,但结果相同。我怎样才能克服这个问题?我使用Windows8和Java1.8.0如何在java中将文件写入特定文件夹?,java,file,directory,bufferedimage,Java,File,Directory,Bufferedimage,我用robot类和BuffereImage类做了这个截图程序。包含截图的.png文件将保存到存在.java文件的文件夹中。我希望将图像文件写入我创建的另一个名为screenshot的文件夹中。我尝试重命名文件,也尝试使用inputstream和outputstream类进行复制,但结果相同。我怎样才能克服这个问题?我使用Windows8和Java1.8.0 package screenshot; //Packages imported for the program import java.
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
}
}
}
要将文件存储到文件夹
屏幕截图
,请更改变量
String ref=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
包名称与目标文件夹的名称相同。更改包名或文件夹名,列出的每个方法都可以正常工作您注释掉了更改目录的行-为什么?我也尝试过了。没有工作,所以我对它们进行了注释。如果您提供一个帮助,以便我们可以实际运行您的代码,那么我们可以提供更多帮助。但是我没有你的
机器人
课程或图像
。。。一个MCVE和更多关于出错原因的详细信息将大有帮助。更改文件名,然后尝试…我认为这是因为文件名..尝试使用临时文件名并测试它。。。
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