導航:首頁 > 文件教程 > java文件寫到本地文件

java文件寫到本地文件

發布時間:2023-03-11 11:11:13

java中如何將file緩存化,然後寫入本地

File file = new File("F:\\私人地帶\\寬頻賬號.txt");
try {
FileInputStream fs = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fs,"GBK"));
String subStr = "";

File toFile = new File("F:\\dd.txt");
if(toFile.exists()) toFile.delete();
toFile.createNewFile();

BufferedWriter bw = new BufferedWriter(new FileWriter(toFile));
while(null != (subStr = br.readLine())){
System.out.println(subStr);
bw.write(subStr);
}
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

❷ Java操作高效並發操作讀寫文件,消息持久化到本地讀取

樓主,如果寫,先判斷要寫多大的文件、然後分段寫,各線程寫自己的段

如果讀,也是先得到文件大小、再分段,然後各線程讀自己的段

❸ java 將頁面內容寫入excel文件中並可以將其下載到本地任意位置

java本身襲要生成excel文件必然是在後台做的,通過poi庫生成excel文件並製作表格。
無法直接通過網頁保存生成excel。
至於下載到本地任意位置,也是後台生成了excel文件發送到前台(瀏覽器),由用戶選擇要存在哪兒,不能直接存儲(這是web沙箱限制,不允許網頁直接訪問本地硬碟,不然你想想,如果你打開一個網頁,網頁代碼可以任意訪問你的硬碟,你還敢開網頁嗎)。
要繞過沙箱限制必須裝插件,也就是,你必須開發一個com或plugin插件,可以訪問本地硬碟,但這需要用戶手工安裝(比如flash的插件,你之所以能用網頁看flash是因為裝了它的插件,但這是你手工裝的,它不能繞過你直接給你裝,它必須詢問你行不行,你要手工點了OK,才能裝)

❹ java 從資料庫取出數據並保存到本地文本中

先看資料庫表, 我裡面有46條記錄,其中有三條重復,我就拿其中一條emp_id 為"

DWR65030M" 做例子

裡面有兩條記錄 ,實現了

❺ Java 復制另一台電腦上的文件到本地主機, 怎麼寫

簡單的方法,先映射成本地驅動器,再用JAVA,就相當本機不同的盤之間拷貝了

❻ java怎麼往本地磁碟上寫文件

這是一個很有用的文件工具類,你可以把他存起來以後使用。
她應該可以滿足你日常文件的基本功能啦!寫文件,讀文件,復制文件,復制文件夾等。
希望是你想要的.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
*
* <p>
* Title: 文件處理工具類
* </p>
* <p>
* Description:實現文件的簡單處理,復制文件、目錄等
* </p>
* <p>
* Copyright: Copyright (c) 2005
* </p>
* <p>
* Company: www.easyjf.com
* </p>
*
* @author 天一
* @version 1.0
*/
public class FileUtil {

/**
* 復制目錄下的文件(不包括該目錄)到指定目錄,會連同子目錄一起復制過去。
*
* @param targetFile
* @param path
*/
public static void FileFromDir(String targetDir, String path) {
File file = new File(path);
createFile(targetDir, false);
if (file.isDirectory()) {
FileToDir(targetDir, listFile(file));
}
}

/**
* 復制目錄下的文件(不包含該目錄和子目錄,只復制目錄下的文件)到指定目錄。
*
* @param targetDir
* @param path
*/
public static void FileOnly(String targetDir, String path) {
File file = new File(path);
File targetFile = new File(targetDir);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File subFile : files) {
if (subFile.isFile()) {
File(targetFile, subFile);
}
}
}
}

/**
* 復制目錄到指定目錄。targetDir是目標目錄,path是源目錄。
* 該方法會將path以及path下的文件和子目錄全部復制到目標目錄
*
* @param targetDir
* @param path
*/
public static void Dir(String targetDir, String path) {
File targetFile = new File(targetDir);
createFile(targetFile, false);
File file = new File(path);
if (targetFile.isDirectory() && file.isDirectory()) {
FileToDir(targetFile.getAbsolutePath() + "/" + file.getName(),
listFile(file));
}
}

/**
* 復制一組文件到指定目錄。targetDir是目標目錄,filePath是需要復制的文件路徑
*
* @param targetDir
* @param filePath
*/
public static void FileToDir(String targetDir, String... filePath) {
if (targetDir == null || "".equals(targetDir)) {
System.out.println("參數錯誤,目標路徑不能為空");
return;
}
File targetFile = new File(targetDir);
if (!targetFile.exists()) {
targetFile.mkdir();
} else {
if (!targetFile.isDirectory()) {
System.out.println("參數錯誤,目標路徑指向的不是一個目錄!");
return;
}
}
for (String path : filePath) {
File file = new File(path);
if (file.isDirectory()) {
FileToDir(targetDir + "/" + file.getName(), listFile(file));
} else {
FileToDir(targetDir, file, "");
}
}
}

/**
* 復制文件到指定目錄。targetDir是目標目錄,file是源文件名,newName是重命名的名字。
*
* @param targetFile
* @param file
* @param newName
*/
public static void FileToDir(String targetDir, File file, String newName) {
String newFile = "";
if (newName != null && !"".equals(newName)) {
newFile = targetDir + "/" + newName;
} else {
newFile = targetDir + "/" + file.getName();
}
File tFile = new File(newFile);
File(tFile, file);
}

/**
* 復制文件。targetFile為目標文件,file為源文件
*
* @param targetFile
* @param file
*/
public static void File(File targetFile, File file) {
if (targetFile.exists()) {
System.out.println("文件" + targetFile.getAbsolutePath() + "已經存在,跳過該文件!");
return;
} else {
createFile(targetFile, true);
}
System.out.println("復制文件" + file.getAbsolutePath() + "到" + targetFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
fos.write(buffer);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static String[] listFile(File dir) {
String absolutPath = dir.getAbsolutePath();
String[] paths = dir.list();
String[] files = new String[paths.length];
for (int i = 0; i < paths.length; i++) {
files[i] = absolutPath + "/" + paths[i];
}
return files;
}

public static void createFile(String path, boolean isFile) {
createFile(new File(path), isFile);
}

public static void createFile(File file, boolean isFile) {
if (!file.exists()) {
if (!file.getParentFile().exists()) {
createFile(file.getParentFile(), false);
} else {
if (isFile) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdir();
}
}
}
}
}

❼ 求解:JAVA中怎麼使用IO流把println中的信息輸出到本地文件中

方法很多,println中的信息,就是一個string

//filePath要寫入的文件地址。append是否在已有的基礎上追加,false的話就是清空這個文件,再寫入。true的話就是在這個文件內容後面追加內容
publicvoidWriteStringToFile2(StringfilePath,booleanappend){
try{
FileWriterfw=newFileWriter(filePath,append);
BufferedWriterbw=newBufferedWriter(fw);
bw.append("println裡面的內容");
bw.write("abc ");
bw.write("def ");
bw.write("hijk");
bw.close();
fw.close();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
  
閱讀全文

與java文件寫到本地文件相關的資料

熱點內容
招標文件中的凈值是什麼意思 瀏覽:675
有哪些app能借出5000 瀏覽:250
編程語言哪個發展好 瀏覽:974
刪除xp密碼 瀏覽:974
手機怎麼在word製作作業文件 瀏覽:489
工行銀行卡安全升級 瀏覽:807
桌面放的文件找不到 瀏覽:922
買學生票用什麼app 瀏覽:590
共建共享網路平台 瀏覽:39
js傳值到超鏈接裡面 瀏覽:608
編程中的w和h是什麼 瀏覽:313
資料庫切了什麼意思 瀏覽:213
如何登錄極路由器設置密碼 瀏覽:522
jsp用戶登陸密碼加密源代碼 瀏覽:629
everfilter使用教程 瀏覽:768
作業票文件名稱是什麼 瀏覽:463
私密文件忘記密碼 瀏覽:686
藏文軟體app怎麼可以下載 瀏覽:960
鍵盤文件名 瀏覽:538
電腦自帶驅動在那個文件夾 瀏覽:531

友情鏈接