「java文本复制」java文件内容复制

博主:adminadmin 2022-12-01 11:52:09 54

本篇文章给大家谈谈java文本复制,以及java文件内容复制对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

怎么从一个java程序里复制文字

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class FileCopy {

static final String fromeFile = "c:\\test1.txt";

static final String toFile = "c:\\test2.txt";

public static void main(String args[]) {

try {

BufferedReader read = new BufferedReader(new FileReader(new File(fromeFile)));

FileWriter write = new FileWriter(new File(toFile), true);

String temp;

while((temp = read.readLine())!=null){

write.write(temp);

}

write.close();

read.close();

System.out.println("内容已从"+fromeFile+"复制追加到"+toFile);

} catch (IOException e) {

e.printStackTrace();

}

}

}

java将两个文本文件的内容复制到另一个文件中

主要是用到java里面的i/o流。代码例子如下:

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader; /** * java读写文件,复制文件 * 读取d:/1.txt文件内容,写入f:/text.txt文件中. * @author young * */public class FileWriterTest { // 读写文件 public static void rwFile(){ FileWriter fw = null; BufferedReader br = null; try { fw = new FileWriter("f:\\text.txt", true); br = new BufferedReader(new InputStreamReader( new FileInputStream("d:\\1.txt"), "UTF-8")); String line = null; while ((line = br.readLine()) != null) { System.out.println("文件内容: " + line); fw.write(line); fw.flush(); } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) { rwFile(); }}

首先在D盘新建文件1.txt,输入任意内容。然后执行java代码即可。

java文件复制粘贴

复制粘贴实际上是文件的流读取和写入可以通过如下方法实现:

读写是两个不同的分支,通常都是分开单独使用的。

可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。

BufferedReader bre = null;

try {

String file = "D:/test/test.txt";

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

while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环

{

System.out.println(str);//原样输出读到的内容

};

备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。

可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:

OutputStreamWriter pw = null;//定义一个流

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

pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write

pw.close();//关闭流

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

java怎么实现复制粘贴功能

1. 往剪切板写文本数据(就是常说的String拉)

Java代码

protected static void setClipboardText(Clipboard clip, String writeMe) {

Transferable tText = new StringSelection(writeMe);

clip.setContents(tText, null);

}

protected static void setClipboardText(Clipboard clip, String writeMe) {

Transferable tText = new StringSelection(writeMe);

clip.setContents(tText, null);

}

2. 从指定的剪切板中获取文本内容

Java代码

protected static String getClipboardText(Clipboard clip) throws Exception{

// 获取剪切板中的内容

Transferable clipT = clip.getContents(null);

if (clipT != null) {

// 检查内容是否是文本类型

if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))

return (String)clipT.getTransferData(DataFlavor.stringFlavor);

}

return null;

}

如何用Java把一个文本中指定信息复制到另一个文本中

你好:

从A读取到B中:

import java.io.*;

public class IoDemo{

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

        String str = "";

        File file1 = new File("D:\\A.txt");

        File file2 = new File("D:\\B.txt");

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file1),"UTF-8"));

        //读取

        while (br.read()!=-1) {

str += br.readLine();

}

        br.close();

        br = new BufferedReader(new FileReader(file2));

        str += br.readLine();

        br.readLine();

        //写入

        BufferedWriter bw = new BufferedWriter(new FileWriter(file2));

        bw.write(str);

        bw.close();

    }

}

Java 将一个文件复制到另一处

test.copy("G:\\G盘寄存资料\\我的文档1\\音乐课堂.doc","G:\\G盘寄存资料");

请注意上面的有个文件夹名字叫“G盘寄存资料”,你复制的文件后的新文件名也叫“G盘寄存资料”,这样名字重复了,所以就出错了。

可以把程序改成这样的话就行了:

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class FileCopy {

public void copy(String src, String dest){//**********

InputStream is=null;

OutputStream os=null;

char ch[]=src.toCharArray();

//************新添加的代码**********

int pos=0;

for(int i=ch.length-1;i=0;i--)

{

if(ch[i]=='\\')

{

if(ipos)

pos=i;

}

}

String temp=src.substring(pos);

dest=dest+temp;

System.out.println("dest="+dest);

//****************************************

try {

is=new BufferedInputStream(new FileInputStream(src));

os=new BufferedOutputStream(new FileOutputStream(dest));

byte[] b=new byte[256];

int len=0;

String str=null;

StringBuilder sb=new StringBuilder();

try {

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

os.write(b,0,len);

}

os.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(is!=null){

try {

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}finally{

if(os!=null){

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

FileCopy test=new FileCopy();

test.copy("G:\\G盘寄存资料\\我的文档1\\hello.txt","G:\\G盘寄存资料");//++++++++++++++++++++++

}

}

关于java文本复制和java文件内容复制的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-01,除非注明,否则均为首码项目网原创文章,转载请注明出处。