導航:首頁 > 編程語言 > 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的字元串復制相關的資料

熱點內容
做賬為什麼要用加密狗 瀏覽:583
考研群體怎麼解壓 瀏覽:156
linux修改命令提示符 瀏覽:224
圓圈裡面k圖標是什麼app 瀏覽:59
pdf加空白頁 瀏覽:945
linux伺服器如何看網卡狀態 瀏覽:316
解壓新奇特視頻 瀏覽:704
圖書信息管理系統java 瀏覽:552
各種直線命令詳解 瀏覽:862
程序員淚奔 瀏覽:146
素材怎麼上傳到伺服器 瀏覽:515
android百度離線地圖開發 瀏覽:189
web可視化編程軟體 瀏覽:292
java筆試編程題 瀏覽:746
win11什麼時候可以裝安卓 瀏覽:564
java不寫this 瀏覽:1001
雲點播電影網php源碼 瀏覽:97
pythonclass使用方法 瀏覽:226
移動加密軟體去哪下載 瀏覽:294
php彈出alert 瀏覽:209