當前位置:首頁 » 編程語言 » java中復制

java中復制

發布時間: 2022-05-30 19:20:18

1. java中復制字元串怎麼復制要求直接輸出

只需要賦值給新的字元串變數就可以了。

java中字元串變數修改時,彼此相互不影響。賦值給新的字元串變數時,原來的字元串變數不會隨著新字元串變數的修改而修改。

示例:

publicvoidshow(){
Strings1="abc";//初始化一個字元串變數s1
Strings2=s1;//把s1表示的字元串復制給s2
s1="cde";//改變s1的字元串內容
System.out.println(s2);//輸出s2,內容還是"abc",不受原來字元串變數的改變而改變
}

2. 使用Java語言如何實現快速文件復制

使用Java語言如何實現快速文件復制:
代碼:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Test {
public static void main(String[] args){
long start = System.currentTimeMillis();
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileChannel inFileChannel = null;
FileChannel outFileChannel = null;
try {
fileInputStream = new FileInputStream(new File("C:\\from\\不是鬧著玩的.flv"));
fileOutputStream = new FileOutputStream(new File("C:\\to\\不是鬧著玩的.flv"));
inFileChannel = fileInputStream.getChannel();
outFileChannel = fileOutputStream.getChannel();
inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);//連接兩個通道,從in通道讀取數據寫入out通道。
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInputStream != null){
fileInputStream.close();
}
if(inFileChannel != null){
inFileChannel.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
if(outFileChannel != null){
outFileChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("視頻文件從「from」文件夾復制到「to」文件需要" + (end - start) + "毫秒。");
}
}

3. java中復制文件的兩種方法是什麼

第一種方法:古老的方式
public static long forJava(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
byte[] buffer=new byte[length];
while(true){
int ins=in.read(buffer);
if(ins==-1){
in.close();
out.flush();
out.close();
return new Date().getTime()-time;
}else
out.write(buffer,0,ins);
}
}

方法的2參數分別是原始文件,和拷貝的目的文件.這里不做過多介紹.

實現方法很簡單,分別對2個文件構建輸入輸出流,並且使用一個位元組數組作為我們內存的緩存器, 然後使用流從f1 中讀出數據到緩存里,在將緩存數據寫到f2裡面去.這里的緩存是2MB的位元組數組

第2種方法:使用NIO中的管道到管道傳輸
public static long forTransfer(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
int i=0;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())<20971520)
length=(int)(inC.size()-inC.position());
else
length=20971520;
inC.transferTo(inC.position(),length,outC);
inC.position(inC.position()+length);
i++;
}
}

實現方法:在第一種實現方法基礎上對輸入輸出流獲得其管道,然後分批次的從f1的管道中像f2的管道中輸入數據每次輸入的數據最大為2MB

4. Java 如何復制對象

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

5. 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、數據中心、游戲控制台、科學超級計算機、行動電話和互聯網,同時擁有全球最大的開發者專業社群。

6. 在java中,如何復制一個對象比如說string對象

可以使用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文檔

7. Java文件復制問題

如下修改

修改2

如果滿意,望採納,謝謝!

8. 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;
}

9. 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。 }}

熱點內容
斯諾克資料庫 發布:2025-02-11 21:54:02 瀏覽:533
安卓手機降噪功能在哪裡打開呢 發布:2025-02-11 21:52:56 瀏覽:701
騰訊雲伺服器購買網址 發布:2025-02-11 21:37:46 瀏覽:61
安卓電話視頻怎麼投電視上 發布:2025-02-11 21:32:27 瀏覽:19
易簽到源碼 發布:2025-02-11 21:31:03 瀏覽:499
編程班會 發布:2025-02-11 21:27:19 瀏覽:739
ubuntu編譯fortran 發布:2025-02-11 21:21:59 瀏覽:202
雲伺服器寬頻單位 發布:2025-02-11 20:48:11 瀏覽:538
安卓數據線公頭是哪個 發布:2025-02-11 20:45:42 瀏覽:812
網址原始密碼是什麼 發布:2025-02-11 20:33:52 瀏覽:72