導航:首頁 > 編程語言 > java的字元串復制

java的字元串復制

發布時間:2022-08-13 16:13:31

『壹』 java中如何將一個字元串復制到一個文本文件中

我寫的一個,能實現寫文件的功能
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class WriteTest{
public static void main(String[] args)throws Exception{
FileOutputStream fos = new FileOutputStream("F:\\Hello.txt",true);//創建一個Hello.txt文本文件在F盤,也可以自己隨便設定,文件名字true表示可以追加寫入
PrintStream ps = new PrintStream(fos);
Scanner in=new Scanner(System.in);
System.out.println("請輸入要寫入文件的信息:");
String str=in.next();
System.setOut(ps);//System 是 java.lang包里的,將流做轉向,即將下面要往控制台輸出的轉向到寫入到文件中
System.out.println(str);//把控制台得到的數據寫入文件
}
}

『貳』 java編程實驗從頭拷貝字元串「 this string until 「g」」直到g並輸出

String str = " this string until \"g\"";
int strLength = str.indexOf('g');
System.out.println(new String(str.getBytes(), 0, strLength));

『叄』 java:字元串拷貝

因為a2為byte數組,byte類型是可以自動向上轉型成char的。
new String(a2) :

String的構造方法只給出了參數是char數組的構造器,
因此本來應該為97,98,99,65,67,68的byte數組轉換為abcACD的char數組了,然後abcACD的char數組變為String字元串

b2也是同樣道理。

『肆』 java中復制字元串

主要部分就1句

text2.setText(text1.getSelectedText());

『伍』 java 字元串數組復制

@param src the source array.
* @param srcPos starting position in the source array.
* @param dest the destination array.
* @param destPos starting position in the destination data.
* @param length the number of array elements to be copied.
System.array(src, srcPos, dest, destPos, length)

『陸』 JAVA TXT字元流復制代碼問題

File file=new File("E:\\1.txt");//創建文件對象,

FileInputStream fread; //FileInputStream 用於讀取諸如圖像數據之類的原始位元組流。要讀取字元流,請考慮使用 FileReader。

BufferedInputStream reader=null;

BufferedOutputStream write=null; //該類實現緩沖的輸出流。通過設置這種輸出流,應用程序就可以將各個位元組寫入底層輸出流中,而不必針對每次位元組寫入調用底層系統。

try
{
fread = new FileInputStream(file);//將文件對象添加到文件的輸入流,也就是你要從你的數據源開始讀取數據

reader=new BufferedInputStream(fread);//將位元組流對象添加到緩存區中,這樣效率要高一點

String str=null;//初始化字元串這樣保存的是讀取的一行的數據

write=new BufferedOutputStream(new FileOutputStream(new File("E:\\2.txt")));//創建的是輸出流緩存 請要輸出的目的地添加到緩存區中,

byte b[]=new byte[1024];//定義每次讀取的位元組的范圍,也是存儲數據的容器

int i=0;

while((i=reader.read(b))!=-1) //下一個數據位元組,如果到達流末尾,則返回 -1。
{
write.write(b);//開始寫入數據
}
}
catch (FileNotFoundException e) //如果沒有文件列印該異常
{
e.printStackTrace();
}
catch (IOException e) //如果出現io異常 也就處理異常
{
e.printStackTrace();
}
finally//最後不管讀取還是沒讀取成功都釋放資源
{
try
{
write.close();//關閉緩存區,關閉緩存區會自動關閉相應的io流
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

閱讀全文

與java的字元串復制相關的資料

熱點內容
碩士英語綜合教程pdf 瀏覽:46
分段加密的安全性 瀏覽:507
咪咕直播為什麼沒有適配安卓系統 瀏覽:172
php模版大全 瀏覽:102
沒車能解壓嗎 瀏覽:634
php開發oa系統源碼 瀏覽:759
怎麼安裝蘋果ios的app 瀏覽:581
app拉新如何機刷 瀏覽:480
zendeclipseforphp 瀏覽:480
同時有幾個微信如何加密微信 瀏覽:86
大眾20t壓縮比 瀏覽:566
程序員要記住的500個單詞 瀏覽:830
wq快捷方式在哪個文件夾 瀏覽:965
雲南到河北源碼 瀏覽:92
安卓手機怎麼玩造夢3 瀏覽:60
多玩我的世界盒子怎麼創造伺服器地址 瀏覽:986
手機如何下載米家app 瀏覽:96
未知來源app在哪裡 瀏覽:206
命令與征服3合集 瀏覽:752
有免稅標簽有溯源碼是假的嗎 瀏覽:218