當前位置:首頁 » 文件管理 » stream寫文件夾

stream寫文件夾

發布時間: 2022-08-07 12:59:04

① 怎麼把指定文件存在指定目錄下java

java寫入文件到指定文件夾的方法主要有兩種:利用PrintStream和利用StringBuffer
例如將文本「I'm the text to be write」寫入到文件夾D:/test下,並命名為test.txt,則兩種方式簡單實現代碼如下:
1. 利用PrintStream寫文件
public void PrintStreamDemo(){
try {
FileOutputStream out=new FileOutputStream("D:/test.txt");
PrintStream p=new PrintStream(out);
p.println("I'm the text to be write");
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
2. 利用StringBuffer寫文件

public void StringBufferDemo() throws IOException{

File file=new File("D:/test.txt");
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);
StringBuffer sb=new StringBuffer();
sb.append("I'm the text to be write");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}

提示:利用StringBuffer寫文件可以設定使用何種編碼,有效解決中文問題。

② 如何將一個Stream類型的流寫入一個文件中

基於流(Stream)的解決
流是單向的有方向性的描述信息流的對象,InputStream是輸入流的介面,對程序來說是入,是讀,可以從文件讀,緩存區讀,網路節點讀等等.
寫入文件,對程序來說是出,是寫,就是FileOutputStream,可以寫入int也可以byte[]
所以解決方案就是從InputStream中讀出內存到byte[]中然後,使用FileOutputStream寫入文件中.比如:其中一種寫法
InputStream is = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("b.txt");
byte[] b = new byte[1024];
while((is.read(b)) != -1){
fos.write(b);
}
is.close();
fos.close();

③ java怎麼把文本內容在指定文件夾里

java寫入文件到指定文件夾的方法主要有兩種:利用PrintStream和利用StringBuffer


例如將文本「I'm the text to be write」寫入到文件夾D:/test下,並命名為test.txt,則兩種方式簡單實現代碼如下:


1. 利用PrintStream寫文件

publicvoidPrintStreamDemo(){
try{
FileOutputStreamout=newFileOutputStream("D:/test.txt");
PrintStreamp=newPrintStream(out);
p.println("I'mthetexttobewrite");
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
}


2. 利用StringBuffer寫文件

publicvoidStringBufferDemo()throwsIOException{
Filefile=newFile("D:/test.txt");
if(!file.exists())
file.createNewFile();
FileOutputStreamout=newFileOutputStream(file,true);
StringBuffersb=newStringBuffer();
sb.append("I'mthetexttobewrite");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}

提示:利用StringBuffer寫文件可以設定使用何種編碼,有效解決中文問題。

④ 如何將OutputStream中的東西寫入文件

out.write(b);不好意思是這句,
wrong1111()寫的是對的,OutputStream out =new FileOutputStream(file);這里關聯了out與file,寫到out就是寫到file

⑤ C# Stream寫文件的問題

Stream是抽象類 不能實例化得 所以 你還是用StreamWrite吧!
public abstract class Stream : System.MarshalByRefObject System.IO 的成員

⑥ 如何把stream文件流轉化為文件

public class FileTest {
public static void main(String[] args) {
File file=new File("c:\\test.txt");
BufferedReader read=null;
BufferedWriter writer=null;
try {
writer=new BufferedWriter(new FileWriter("c:\\zwm.txt"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
read=new BufferedReader(new FileReader(file));
String tempString = null;
while((tempString=read.readLine())!=null){
writer.append(tempString);
writer.newLine();//換行
writer.flush();//需要及時清掉流的緩沖區,萬一文件過大就有可能無法寫入了
}
read.close();
writer.close();
System.out.println("文件寫入完成...");
} catch (IOException e) {
e.printStackTrace();
}

}
}

這個例子是將一個文件的東西,寫入到另一個文件裡面去。
你可以把你讀取到的流用Out流寫到你的文件里

⑦ 怎樣用FileOutputStream寫一個指定類型的文件

String fileName = "d:\\hello.txt";
File file = new File(fileName);
FileOutputStream fos = new FileOutputSream(file);
然後你再試試

一樣的呀,你把值取二次呀,第一次取出文件名來,第二次取出*.txt
然後你"*.txt".substring(1,"*.txt".length());取出".txt"
再把 fileName+".txt"就好了呀

⑧ 怎麼使用FileOutputStream在指定位置寫入文件

* 拷貝文件,
* @param oldPath 舊文件路徑
* @param newPath 新文件路徑
* @throws Exception
*/
private void File(String oldPath, String newPath) throws Exception{

int bytesum = 0;
int byteread = 0;

File oldFile = new File(oldPath);
InputStream in = null;
OutputStream out = null;

if(oldFile.exists()){

try {

in = new FileInputStream(oldPath);
out = new FileOutputStream(newPath);

byte[] buffer = new byte[1024*5];

while((byteread = in.read(buffer)) != -1){

bytesum += byteread;
System.out.println(bytesum);
out.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("輸入myeclipse的路徑不正確");
}finally{

if(in != null)
in.close();
if(out != null)
out.close();
}
}

}

⑨ C# 一個StreamWrite寫入多個文件

一般情況下不能用一個 FileStream 和 StreamWrite控制兩個文件,除非第一個文件已經關閉

熱點內容
怎麼樣更改無線網密碼 發布:2024-04-20 13:53:23 瀏覽:883
python用戶 發布:2024-04-20 13:51:46 瀏覽:748
光遇蘋果如何下載安卓服 發布:2024-04-20 13:40:03 瀏覽:460
半歲學編程 發布:2024-04-20 13:17:53 瀏覽:217
linux按鍵 發布:2024-04-20 13:17:52 瀏覽:832
最頂配的伺服器能容納多少ip 發布:2024-04-20 13:17:44 瀏覽:514
貸款車解壓需要什麼 發布:2024-04-20 13:14:52 瀏覽:72
安卓手機電腦怎麼切換 發布:2024-04-20 13:13:25 瀏覽:893
android交叉編譯環境 發布:2024-04-20 13:00:10 瀏覽:184
伺服器怎麼搭建中間層 發布:2024-04-20 12:40:02 瀏覽:98