導航:首頁 > 編程語言 > 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 瀏覽:902
sm3演算法實現java 瀏覽:672
只有程序員才懂的笑話 瀏覽:264
php開發app介面源碼 瀏覽:9
加密狗寶寶 瀏覽:234
單片機搶答器的原理 瀏覽:925
百利達體脂儀可以用什麼app連接 瀏覽:223
伺服器2016自動備份怎麼取消 瀏覽:843
dos攻擊命令大全 瀏覽:842
夢世界伺服器怎麼擴大領地 瀏覽:971
解壓breathe 瀏覽:436
webview調用java 瀏覽:351
android待機流程 瀏覽:859
python直接退出程序 瀏覽:851
百戰程序員收費標准 瀏覽:778
時鍾置換演算法指針變化規則 瀏覽:253
微信加密能否改密碼 瀏覽:112
android許可權組 瀏覽:178
2017單片機 瀏覽:484
讓孩子感興趣編程的電影 瀏覽:267