import java.io.FileNotFoundException;

網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì),成都做網(wǎng)站公司-成都創(chuàng)新互聯(lián)公司已向超過千家企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營(yíng)銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價(jià)格為您打造企業(yè)品質(zhì)網(wǎng)站。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class CreateFile {
public static void main(String[] args) {
String str = "需要寫入的字";
String fileName = "D:\\a\\a.xml";
OutputStream output = null;// 輸出字節(jié)流
OutputStreamWriter outputWrite = null;// 輸出字符流
PrintWriter print = null;// 輸出緩沖區(qū)
try {
output = new FileOutputStream(fileName);
outputWrite = new OutputStreamWriter(output);
print = new PrintWriter(outputWrite);
print.print(str);
print.flush();// 一定不要忘記此句,否則數(shù)據(jù)有可能不能被寫入文件
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
再一個(gè)問題就是只要你編碼正確就可以正常打開。
public void createFile(){\x0d\x0a\x0d\x0a//path表示你所創(chuàng)建文件的路徑\x0d\x0aString path = "d:/tr/rt";\x0d\x0aFile f = new File(path);\x0d\x0aif(!f.exists()){\x0d\x0a f.mkdirs();\x0d\x0a} \x0d\x0a// fileName表示你創(chuàng)建的文件名;為txt類型;\x0d\x0aString fileName="test.txt";\x0d\x0aFile file = new File(f,fileName);\x0d\x0aif(!file.exists()){\x0d\x0atry {\x0d\x0afile.createNewFile();\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0a//現(xiàn)在你可以在d:/tr/rt 目錄下找到test.txt文件
srcFile:源文件路徑
desFile:目標(biāo)文件路徑
public static void copyFileByBuffer(String srcFile, String desFile) throws IOException {
Test test = new Test();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
long s = 0;
try {
// 建立源文件與代碼之間的流(輸入流)
bis = new BufferedInputStream(new FileInputStream(new File(srcFile)));
// 得到源文件的大小
s = bis.available();
// 保存到多線程方法中
test.setS(s);
// 建立目標(biāo)文件與代碼之間的流(輸出流)
bos = new BufferedOutputStream(new FileOutputStream(new File(desFile)));
//
int BUFFER_SIZE = 16 * 1024;
test.setLen(BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
// 從輸入流里讀取源文件信息
int len = bis.read();
while (len 0) {
// 把讀到的源文件信息以輸出流的方式寫出去
bos.write(buffer, 0, len);
bis.read(buffer);
}
} finally {
// 關(guān)閉輸出流
if (bos != null) {
bos.close();
}
// 關(guān)閉輸入流
if (bis != null) {
bis.close();
}
}
}
public void createFile(){
//path表示你所創(chuàng)建文件的路徑
String path = "d:/tr/rt";
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
// fileName表示你創(chuàng)建的文件名;為txt類型;
String fileName="test.txt";
File file = new File(f,fileName);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//現(xiàn)在你可以在d:/tr/rt 目錄下找到test.txt文件
網(wǎng)頁(yè)標(biāo)題:手機(jī)java創(chuàng)建文件代碼 手機(jī)編寫java代碼
當(dāng)前地址:http://www.yijiale78.com/article18/dooeigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、App開發(fā)、企業(yè)建站、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)公司、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)