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

新闻中心

这里有您想知道的互联网营销解决方案
java复制图片主要代码,可以复制代码的图片

从文件中读取图片和写入图片到文件里的java代码是什么?

首先导入各种需要的包:

公司主营业务:网站设计制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出睢阳免费做网站回馈大家。

import java.awt.Image;

import javax.imageio.ImageIO;

import java.io.*;

读取图片的方法如下:

Image[] array = new Image[10];

Image image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧

array[0] = image;

图片读出来了。

如果你有一个Image对象,想把它写入文件可以这样做:

BufferedImage image = ImageIO.read(new File("d:\\source.gif"));

//要想保存这个对象的话你要把image声明为BufferedImage 类型

ImageIO.write(image, "png", new File("f:\\test.png"));

java图形用户界面的选择一个文件并复制(另存为)的代码,麻烦了。

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import java.awt.Font;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.Color;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import javax.swing.JFileChooser;

public class CopyFile {

private JFrame frame;

private JTextField textField;

private JTextField textField_1;

private JFileChooser chooser;

private String readPath;

private String writePath;

/**

 * Launch the application.

 */

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

CopyFile window = new CopyFile();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

 * Create the application.

 */

public CopyFile() {

initialize();

}

/**

 * Initialize the contents of the frame.

 */

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 545, 277);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabel label = new JLabel("\u6587\u4EF6\uFF1A");

label.setFont(new Font("黑体", Font.BOLD, 18));

label.setBounds(26, 68, 57, 25);

frame.getContentPane().add(label);

JLabel lblNewLabel = new JLabel("\u4FDD\u5B58\u76EE\u5F55\uFF1A");

lblNewLabel.setFont(new Font("黑体", Font.BOLD, 18));

lblNewLabel.setBounds(10, 119, 95, 25);

frame.getContentPane().add(lblNewLabel);

textField = new JTextField();

textField.setBounds(105, 68, 299, 25);

frame.getContentPane().add(textField);

textField.setColumns(10);

textField_1 = new JTextField();

textField_1.setBounds(105, 121, 299, 25);

frame.getContentPane().add(textField_1);

textField_1.setColumns(10);

chooser = new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);// 设置选择模式,既可以选择文件又可以选择文件夹

JButton button = new JButton("\u6253\u5F00");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int index = chooser.showOpenDialog(null);

chooser.setDialogType(JFileChooser.OPEN_DIALOG);

chooser.setMultiSelectionEnabled(false);

chooser.setAcceptAllFileFilterUsed(false);

if (index == JFileChooser.APPROVE_OPTION) {

// 把获取到的文件的绝对路径显示在文本编辑框中

textField.setText(chooser.getSelectedFile()

.getAbsolutePath());

readPath = textField.getText();

}

}

});

button.setFont(new Font("黑体", Font.BOLD, 18));

button.setBounds(432, 67, 87, 26);

frame.getContentPane().add(button);

JButton button_1 = new JButton("\u6D4F\u89C8");

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int index = chooser.showSaveDialog(null);

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

chooser.setDialogType(JFileChooser.SAVE_DIALOG);

chooser.setMultiSelectionEnabled(false);

chooser.setAcceptAllFileFilterUsed(false);

if (index == JFileChooser.APPROVE_OPTION) {

// 把获取到的文件的绝对路径显示在文本编辑框中

textField_1.setText(chooser.getSelectedFile()

.getAbsolutePath());

writePath = textField_1.getText() + "\\";

}

}

});

button_1.setFont(new Font("黑体", Font.BOLD, 18));

button_1.setBounds(432, 118, 87, 26);

frame.getContentPane().add(button_1);

JButton button_2 = new JButton("\u53E6\u5B58\u4E3A");

button_2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

readPath = textField.getText();

writePath = textField_1.getText() + "\\";

if(copyFile(readPath, writePath)== -1){//原文件不存在

JOptionPane.showMessageDialog(null, "源文件不存在", "警告", JOptionPane.ERROR_MESSAGE);

}

}

});

button_2.setForeground(Color.RED);

button_2.setFont(new Font("黑体", Font.BOLD, 18));

button_2.setBounds(213, 180, 93, 34);

frame.getContentPane().add(button_2);

}

/*

 * *

 * 复制单个文件

 * 

 * @param oldPath String 原文件路径 如:c:/fqf.txt

 * 

 * @param newPath String 复制后路径 如:f:/fgf.txt

 * 

 * @return int 0表示成功,-1表示原文件不存在,-2表示未知错误。

 */

public int copyFile(String oldPath, String newPath) {

try {

int bytesum = 0;

int byteread = 0;

File oldfile = new File(oldPath);

if (oldfile.exists()) { // 文件存在时

InputStream inStream = new FileInputStream(oldPath); // 读入原文件

System.out.println(newPath);

if(isExist(newPath)){

FileOutputStream fs = new FileOutputStream(newPath);

byte[] buffer = new byte[1444];

while ((byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; // 字节数 文件大小

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

fs.close();

return 0;

}else{

return -2;

}

}

return -1;

} catch (Exception e) {

System.out.println("复制单个文件操作出错");

e.printStackTrace();

return -2;

}

}

public static boolean isExist(String filePath) {

String paths[] = filePath.split("\\\\");

String dir = paths[0];

for (int i = 0; i  paths.length - 2; i++) {// 注意此处循环的长度

try {

dir = dir + "/" + paths[i + 1];

File dirFile = new File(dir);

if (!dirFile.exists()) {

dirFile.mkdir();

System.out.println("创建目录为:" + dir);

}

} catch (Exception err) {

System.err.println("ELS - Chart : 文件夹创建发生异常");

}

}

File fp = new File(filePath);

if (!fp.exists()) {

return true; // 文件不存在,执行下载功能

} else {

return false; // 文件存在不做处理

}

}

}

提取图片地址的java脚本代码

public List getPicList(String str) {

List list = new ArrayList();

Pattern p = Pattern.compile("src\=.+?\.(gif|jpg)");

Matcher m = p.matcher(str);

while(m.find()) {

list.add(m.group());

}

return list;

}

Java代码拷贝文件夹 注:复制文件夹

Java代码复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法,代码如下:

import java.io.*;  

/** 

* 复制文件夹或文件夹 

*/  

public class CopyDirectory {  

// 源文件夹   

static String url1 = "F:/photos";  

// 目标文件夹   

static String url2 = "D:/tempPhotos";  

public static void main(String args[]) throws IOException {  

// 创建目标文件夹   

(new File(url2)).mkdirs();  

// 获取源文件夹当前下的文件或目录   

File[] file = (new File(url1)).listFiles();  

for (int i = 0; i  file.length; i++) {  

if (file[i].isFile()) {  

// 复制文件   

copyFile(file[i],new File(url2+file[i].getName()));  

}  

if (file[i].isDirectory()) {  

// 复制目录   

String sourceDir=url1+File.separator+file[i].getName();  

String targetDir=url2+File.separator+file[i].getName();  

copyDirectiory(sourceDir, targetDir);  

}  

}  

}  

// 复制文件   

public static void copyFile(File sourceFile,File targetFile)   

throws IOException{  

// 新建文件输入流并对它进行缓冲   

FileInputStream input = new FileInputStream(sourceFile);  

BufferedInputStream inBuff=new BufferedInputStream(input);  

// 新建文件输出流并对它进行缓冲   

FileOutputStream output = new FileOutputStream(targetFile);  

BufferedOutputStream outBuff=new BufferedOutputStream(output);  

// 缓冲数组   

byte[] b = new byte[1024 * 5];  

int len;  

while ((len =inBuff.read(b)) != -1) {  

outBuff.write(b, 0, len);  

}  

// 刷新此缓冲的输出流   

outBuff.flush();  

//关闭流   

inBuff.close();  

outBuff.close();  

output.close();  

input.close();  

}  

// 复制文件夹   

public static void copyDirectiory(String sourceDir, String targetDir)  

throws IOException {  

// 新建目标目录   

(new File(targetDir)).mkdirs();  

// 获取源文件夹当前下的文件或目录   

File[] file = (new File(sourceDir)).listFiles();  

for (int i = 0; i  file.length; i++) {  

if (file[i].isFile()) {  

// 源文件   

File sourceFile=file[i];  

// 目标文件   

File targetFile=new File(new File(targetDir).getAbsolutePath()+File.separator+file[i].getName());  

copyFile(sourceFile,targetFile);  

}  

if (file[i].isDirectory()) {  

// 准备复制的源文件夹   

String dir1=sourceDir + "/" + file[i].getName();  

// 准备复制的目标文件夹   

String dir2=targetDir + "/"+ file[i].getName();  

copyDirectiory(dir1, dir2);  

}  

}  

}  

}

java输入输出流拷贝图片 下面的代码为什么生成的图片打不开还会越来越大

while((n=fis.read(buf))!=-1)

{

fos.write(buf, 0, n);//输出到指定文件

}

java 代码实现复制粘粘功能,详细细节如图,我自己写了一段代码,搞了半天没搞好。

如果是JTable.等java图形界面的组件,那么获取数据,赋值都比较简单.

但是看图片,是要写一个Excel的辅助功能, 这对java来说还是有点麻烦了.

最优建议:

Excel的功能. 那么最佳的建议,是使用vba 语言进行扩展.(微软出品,简单,方便,代码量极少) .

其次的建议:

C/C++ 键盘钩子 , 当读取到按键F9时 ,模拟键盘的复制粘贴等操作.

不推荐java , 但java也能勉强凑合解决这个问题:

因为java 很难获取系统底层的按键, Robot也很有局限, 比如窗口失去焦点的时候,读取不到F9按键.   所以java需要调用JNI c语言 比较繁琐. 比较简单的是调用JNA了,但代码量也不少.

当然了如果非要用java写,也可以,我手写了一个简单的JNA+Robot配合

效果图


分享名称:java复制图片主要代码,可以复制代码的图片
URL链接:http://scyingshan.cn/article/hoeihe.html