導航:首頁 > 編程語言 > java復制

java復制

發布時間:2022-01-14 05:58:52

java怎麼實現文件拷貝

工具/原料

一台配置了java環境的電腦

一款適合自己的開發集成環境,這里用的是eclipse Kepler


文件拷貝DEMO

1.首先,理清思路,然後我們再動手操作。

拷貝,有源文件,和目的文件。

如果原文件不存在,提示,報錯。

如果目的文件不存在,創建空文件並被覆蓋。

如果目的地址,也即目的路徑不存在,創建路徑。

拷貝,輸入流,輸出流,關閉流。

拷貝前輸出文件大小,計算拷貝大小,比較並核實。輸出。

② Java 如何復制對象

可以使用clone來實現,clone用於為引用類型的復制
1.使用clone方法的類必須先實現Cloneable介面,不然clone方法會直接返回CloneNotSupportedException不支持克隆的異常
2、實現Cloneable介面的類應該使用公共方法重寫 Object.clone(它是受保護的)。某個對象實現了此介面就克隆它是不可能的。即使 clone 方法是反射性調用的,也無法保證它將獲得成功。
3、在Java.lang.Object類中克隆方法是這么定義的:
protected Object clone()
throws CloneNotSupportedException
創建並返回此對象的一個副本。表明是一個受保護的方法,同一個包中可見。

③ java中數組復制的方法有幾種

最簡單的一種就是直接挨個把原數組的值賦給新數組 不過一般都用System.array(原數組起始復制的標號,新數組接收復制的起始標號,賦值的長度) 這個方法

例如:public class llx {
public static void main(String args[]) {
int a[] = {1,2,3,4,5};
int b[] = new int[10];//搞一個10位置的新數組
System.array(a[0],b[0],a.length);//從a的第一個位置開始復制;從b的第一個位置開始接收;一共接收a的總長度(a.length);;;懂了嗎?這樣的話 b的前5個值就被傳遞了,但是後5個是初始值0。
}
}

補充:Java是一種可以撰寫跨平台應用軟體的面向對象的程序設計語言。Java 技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、游戲控制台、科學超級計算機、行動電話和互聯網,同時擁有全球最大的開發者專業社群。

④ java怎麼復制一個一樣的對象

可以使用clone來實現,clone用於為引用類型的復制
1.使用clone方法的類必須先實現Cloneable介面,不然clone方法會直接返回CloneNotSupportedException不支持克隆的異常
2、實現Cloneable介面的類應該使用公共方法重寫 Object.clone(它是受保護的)。某個對象實現了此介面就克隆它是不可能的。即使 clone 方法是反射性調用的,也無法保證它將獲得成功。
3、在Java.lang.Object類中克隆方法是這么定義的:
protected Object clone()
throws CloneNotSupportedException
創建並返回此對象的一個副本。表明是一個受保護的方法,同一個包中可見。
按照慣例,返回的對象應該通過調用 super.clone 獲得。

詳見關於clone的API文檔

⑤ java中如何實現快速復制文件

public class IOTest2 {

/**
* @author jiang
* @param args
* BufferedReader
* BufferedWriter
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//一次能讀取一行 readLine()方法
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("1.txt")));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("2.txt")));
String str=null;
while((str=br.readLine())!=null){
//文件末尾讀取為null就結束
bw.write(str);
}
bw.flush();//寫入後刷新
bw.close();//關閉文件
br.close();//關閉文件
}
}

⑥ 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句

text2.setText(text1.getSelectedText());

⑧ java如何實現文件的復制粘貼

打開D盤,點編輯,全部選定,右鍵點變籃的文件選復制,打開E盤右鍵點空白處選粘貼。

⑨ java 如何拷貝

package czday0017;

import java.io.*;
import java.util.*;
/** IO 工具類 */
public class IO {
/**
* 獲取目錄的全部文件
* @param dir
* @return
*/
public static List<File> listFile(File dir){
return null;
}
/**
* 獲取目錄的全部文件, 指定擴展名的文件
* @param dir
* @return
*/
public static List<File> listFile(
File dir, String ext){
return null;
}
/**
* 遞歸獲取目錄的全部文件
* @param dir
* @return
*/
public static List<File> listAll(
File dir){
return null;
}
/**
* 遞歸獲取目錄的全部文件, 指定擴展名的文件
* @param dir
* @return
*/
public static List<File> listAll(
File dir, String ext){
return null;
}

/**
* 復制文件
*/
public static void cp(String from, String to)
throws IOException {
cp(new File(from), new File(to));
}
/**
* 復制文件
*/
public static void cp(File from, File to)
throws IOException {
cp(new FileInputStream(from),
new FileOutputStream(to));
}
/**
* 復制文件
*/
public static void cp(InputStream in,
OutputStream out)
throws IOException {
//1K byte 的緩沖區!
byte[] buf = new byte[1024];
int count;
while((count=in.read(buf))!=-1){
System.out.println(count);
out.write(buf, 0, count);
}
in.close();
out.close();
}
/**
* 從流中讀取一行文本, 讀取到一行的結束為止
* @param in
* @return 一行文本
*/
public static String readLine(
InputStream in, String charset)
throws IOException{
byte[] buf = {};
int b;
while(true){
b = in.read();
if(b=='\n' || b=='\r' || b==-1){//編碼是否是回車換行
break;
}
buf=Arrays.Of(buf, buf.length+1);
buf[buf.length-1]=(byte)b;
}
if(buf.length==0 && b==-1)
return null;
return new String(buf,charset);
}

/**
* 讀取文件的全部內容到一個byte數組
* 可以緩存一個"小"文件到堆內存中
*/
public static byte[] read(String filename)
throws IOException{
return read(new File(filename));
}
/**
* 讀取文件的全部內容到一個byte數組
* 可以緩存一個"小"文件到堆內存中
*/
public static byte[] read(File file)
throws IOException{
//用RAF打開文件
RandomAccessFile raf =
new RandomAccessFile(file, "r");
//安裝文件的長度開辟 緩沖區數組(byte數組)
byte[] buf = new byte[(int)raf.length()];
//讀取文件的緩沖區
raf.read(buf);
//關閉文件(RAF)
raf.close();
//返回緩沖區數組引用.
return buf;
}
/**
* 讀取文件的全部內容到一個byte數組
* 可以緩存一個"小"文件到堆內存中
* 如: 文件內容: ABC中 讀取為: {41, 42, 43, d6, d0}
*/
public static byte[] read(InputStream in)
throws IOException{
byte[] ary = new byte[in.available()];
in.read(ary);
in.close();
return ary;
}
/**
* 連接byte 數組的全部內容為字元串,
* 以hex(十六進制)形式連接
* 如: 數組{0x41, 0x42, 0x43, 0xd6, 0xd0}
* 結果: "[41, 42, 43, d6, d0]"
*/
public static String join(byte[] ary){
if(ary==null || ary.length==0)
return "[]";
StringBuilder buf =
new StringBuilder();
buf.append("[").append(
leftPad(Integer.toHexString(ary[0]&0xff),'0',2));
for(int i=1; i<ary.length; i++){
String hex=Integer.toHexString(ary[i]&0xff);
buf.append(",").append(leftPad(hex,'0',2));
}
buf.append("]");
return buf.toString();
}

public static String toBinString(byte[] ary){
if(ary==null || ary.length==0)
return "[]";
StringBuilder buf =
new StringBuilder();
buf.append("[").append(
leftPad(Integer.toBinaryString(ary[0]&0xff),'0',8));
for(int i=1; i<ary.length; i++){
String hex=Integer.toBinaryString(ary[i]&0xff);
buf.append(",").append(leftPad(hex,'0',8));
}
buf.append("]");
return buf.toString();
}
/**
* 實現leftPad功能, 對字元串實現左填充
* @param str 被填充字元串: 5
* @param ch 填充字元: #
* @param length 填充以後的長度: 8
* @return "#######5"
*/
public static String leftPad(
String str, char ch, int length){
if(str.length() == length){
return str;
}
char[] chs = new char[length];
Arrays.fill(chs, ch);
System.array(str.toCharArray(), 0, chs,
length - str.length(), str.length());
return new String(chs);
}
/**
* 將text追加到文件 filename的尾部
* 使用系統默認文本編碼
*/
public static void println (
String filename, String text)
throws IOException{
println(new File(filename),text);
}
public static void println(
File file, String text)throws IOException{
OutputStream out = new FileOutputStream(file,true);
println(out, text);
out.close();
}
/**
* 向流中輸出一行字元串, 使用默認編碼
* 不關閉流
* @param out 目標流
* @param text 文本
* @throws IOException
*/
public static void println(
OutputStream out, String text)throws IOException{
out.write(text.getBytes());
out.write('\n');
}
/**
* 向流中輸出一行字元串, 使用指定的編碼
* 不關閉流
* @param out 目標流
* @param text 文本
* @param charset 指定的編碼
* @throws IOException
*/
public static void println(
OutputStream out, String text, String charset)throws IOException{
out.write(text.getBytes(charset));
out.write('\n');
}
/**
* 文件切分
* @param file
* @param size
* @throws IOException
*/
public static void spilt(String file,int size) throws IOException{
if(size<=0){
throw new IllegalArgumentException("干嗎啊!輸入有誤阿!");
}
int idx = 0;//文件序號
InputStream in = new BufferedInputStream(new FileInputStream(file));
OutputStream out = new BufferedOutputStream(new FileOutputStream(file+"."+idx++));
int b;
int count = 0;
while((b = in.read())!=-1){
out.write(b);
count++;
if(count%(size*1024)==0 ){
out.close();
out = new BufferedOutputStream(new FileOutputStream(file+"."+idx++));
}
}
in.close();
out.close();
}
/**
* 將文件進行連接
* @param filename是一個文件名;如:file.dat.0
*/
public static void join(String file)throws IOException{
String filename = file.substring(0, file.lastIndexOf("."));
String num = file.substring(file.lastIndexOf(".")+1);
int idx = Integer.parseInt(num);
OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
File f = new File(filename+"."+idx++);
while(f.exists()){
InputStream in = new BufferedInputStream(new FileInputStream(f));
cp(in,out);
in.close();
f = new File(filename+"."+idx++);
}
out.close();
}
/**
* 序列化對象
*/
public static byte[] Serialize(Serializable obj)throws IOException{
return null;
}

public static Object unSerializable(byte[] data)throws IOException{
return null;
}

public static Object clone(Serializable obj)throws IOException{
return unSerializable(Serialize(obj)) ;
}
}

//使用cp工具!

⑩ java 怎樣復制一個類

直接無視1樓
實現Cloneable介面,重載clone方法即可
public class A implements Cloneable {
public String name;

public Object clone() {
A o = null;
try {
o = (A) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}

}

閱讀全文

與java復制相關的資料

熱點內容
程序員簡歷幾頁好 瀏覽:288
游俠下載的游戲都需要解壓沒 瀏覽:83
初次認識控制命令完整版 瀏覽:257
雙屏程序員 瀏覽:801
怎麼把兩個文件夾放到一個文件夾裡面 瀏覽:547
命令與征服大神宮 瀏覽:207
php發送簡訊驗證碼 瀏覽:505
前端伺服器如何接收http請求 瀏覽:796
程序員資質查詢 瀏覽:357
程序員被別人開除怎麼辦 瀏覽:888
解壓視頻看一下 瀏覽:129
android仿知乎日報 瀏覽:335
為什麼前端比安卓手機需求大 瀏覽:855
命令行執行關機命令 瀏覽:52
在學校心情不好怎麼解壓 瀏覽:116
我的世界基岩版伺服器怎麼讀取 瀏覽:161
快件命令 瀏覽:853
阿里雲06折伺服器能用嗎 瀏覽:421
h5個人中心源碼 瀏覽:221
下三角矩陣的壓縮存儲 瀏覽:922