當前位置:首頁 » 文件管理 » java上傳txt

java上傳txt

發布時間: 2023-03-26 09:24:57

java已成功連接了linux ftp伺服器並成功上傳txt文件,但是文件為空

ftp.storeFile("ftptest2014.txt",fis);代碼後,添加fis.close();這一代碼試試,另外,你看看的文件是否在D盤存在

⑵ java代碼 如何向TXT文件寫入內容

向txt文件寫入內容基本思路就是獲得一個file對象,新建一個txt文件,打開I/O操作流,使用寫入方法進行讀寫內容,示例如下:

packagecommon;

importjava.io.*;

importjava.util.ArrayList;

publicclassIOTest{

publicstaticvoidmain(Stringargs[]){

ReadDate();

WriteDate();

}

/**

*讀取數據

*/

publicstaticvoidReadDate(){

Stringurl=「e:/2.txt」;

try{

FileReaderread=newFileReader(newFile(url));

StringBuffersb=newStringBuffer();

charch[]=newchar[1024];

intd=read.read(ch);

while(d!=-1){

Stringstr=newString(ch,0,d);

sb.append(str);

d=read.read(ch);

}

System.out.print(sb.toString());

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

/**

*寫入數據

*/

publicstaticvoidWriteDate(){

try{

Filefile=newFile(「D:/abc.txt」);

if(file.exists()){

file.delete();

}

file.createNewFile();

BufferedWriteroutput=newBufferedWriter(newFileWriter(file));

ArrayListResolveList=newArrayList();

for(inti=0;i<10;i++){

ResolveList.add(Math.random()*100);

}

for(inti=0;i

output.write(String.valueOf(ResolveList.get(i))+「 」);

}

output.close();

}catch(Exceptionex){

System.out.println(ex);

}

}

}

原文出自【比特網】,轉載請保留原文鏈接:http://soft.chinabyte.com/database/303/12439303.shtml

⑶ java 怎麼將數據寫入TXT文件

定義一個輸出文件,然後輸出就可以了,具體見下面的代碼

importjava.io.*;

publicclassStreamDemo

{

publicstaticvoidmain(Stringargs[])

{

Filef=newFile("c:\temp.txt");

OutputStreamout=null;

try

{

out=newFileOutputStream(f);

}

catch(FileNotFoundExceptione)

{

e.printStackTrace();

}

//將字元串轉成位元組數組

byteb[]="HelloWorld!!!".getBytes();

try

{

//將byte數組寫入到文件之中

out.write(b);

}

catch(IOExceptione1)

{

e1.printStackTrace();

}

try

{

out.close();

}

catch(IOExceptione2)

{

e2.printStackTrace();

}//以下為讀文件操作

InputStreamin=null;

try

{

in=newFileInputStream(f);

}

catch(FileNotFoundExceptione3)

{

e3.printStackTrace();

}

//開辟一個空間用於接收文件讀進來的數據

byteb1[]=newbyte[1024];

inti=0;

try

{

//將b1的引用傳遞到read()方法之中,同時此方法返回讀入數據的個數

i=in.read(b1);

}

catch(IOExceptione4)

{

e4.printStackTrace();

}

try

{

in.close();

}

catch(IOExceptione5)

{

e5.printStackTrace();

}

//將byte數組轉換為字元串輸出

System.out.println(newString(b1,0,i));

}

}

⑷ java 怎麼導入txt的數據

txt文件里格式是什麼樣的?

import java.nio.file.*;
import java.io.*;
import java.util.stream.*;
try(var writer = new PrintWriter("/tmp/numbers.txt")){
IntStream.rangeClosed(200, 20000).forEach(writer::println); //生成一個每行一個整數的文本文件
Files.lines(Paths.get("/tmp/numbers.txt")).mapToInt(Integer::parseInt).toArray(); // 將文件中的數字讀到一個數組里
} catch(Exception e){
e.printStackTrace();
}

⑸ Java 程序做文件(excle,txt)上傳,然後在刪除的問題

我覺得一樓說的對,我剛才試了下,就是這個原因,你可以參看我寫的一個程序:

========================Code begin ==========================
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* @author limingatcn
* @time Jan 22, 2009 10:11:59 AM
* @version 1.0
*/
public class TestDelete {

public void readInfoFromFile() {
// 先聲明以備用
File file = new File("c:\\test.txt");
FileInputStream fis = null;
byte[] b = null;
int i = 0;

// 處理
try {
fis = new FileInputStream(file);
// byte數組不要越界
b = new byte[123456];
while (fis.available() > 0) {
i += fis.read(b);
}
String str = new String(b, 0, i);
System.out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 執行刪除操作
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
file.delete();
System.out.println("The file was removed.");
}

}

/**
* @param args
*/
public static void main(String[] args) {
new TestDelete().readInfoFromFile();
}

}

========================Code end ============================

在finally中要執行 關閉你所打開讀流的對象 的操作,

在這個程序中如果沒有finally中的fis.close()
test.txt就出現你說的刪除不掉的情況
結合這個程序你再試試。

⑹ java 上傳文件必須是txt文件

$(function(){
new AjaxUpload("#file",{
action:"/dzj/upload?filefolder=monitor/images",
autoSubmit:true,
type:"POST",
name:"file",
onSubmit:function(filepic, extension){
if (extension && /^(txt)$/.test(extension))
{
$("#loading").html("<img src='images/loading.gif'><font color='red'>文件正在上傳...</font>");
$("#loading").show();
}
else
{
$("#loading").html("<font color='red' >請選擇txt文件。</font>");
$("#loading").show();
return false;
}
},
onComplete:function(filepic, response){
$("#loading").html("圖片上傳成功");
$("#loading").show();
var doctitle=document.getElementById("doctitle");
doctitle.value=filepic;
var docurl=document.getElementById("image");
docurl.value=response;
var filepicture=document.getElementById("filepic");
filepicture.value=filepic;
}
});

});

<input type='text' name="file" id="file" required="true" class="easyui-validatebox required"/>
<span id="loading"></span>
<input type='hidden' name="doctitle" id="doctitle" />
<input type='hidden' name="image" id="image" />

⑺ JAVA利用commons.net.ftp.FTPClient的storeFileStream方法TXT文件上傳. 已經轉碼UTF-16LE,上傳後內容亂碼

伺服器端也要支持 utf-16LE 才行,,,,,一般是支持utf-8的

~~~~~~~

熱點內容
美嘉演算法口訣 發布:2025-05-16 06:03:15 瀏覽:952
c程序編譯連接 發布:2025-05-16 06:02:36 瀏覽:964
腳本魔獸 發布:2025-05-16 06:01:52 瀏覽:330
文件夾python 發布:2025-05-16 06:01:43 瀏覽:627
電腦我的世界伺服器游戲幣 發布:2025-05-16 05:27:25 瀏覽:487
索尼手機為什麼不能用安卓10 發布:2025-05-16 05:18:46 瀏覽:784
蔚來es6選擇哪些配置實用 發布:2025-05-16 05:18:05 瀏覽:130
小米如何掃碼wifi密碼 發布:2025-05-16 05:13:38 瀏覽:807
樓層密碼是什麼意思 發布:2025-05-16 05:13:37 瀏覽:13
創建文件夾失敗 發布:2025-05-16 05:12:59 瀏覽:396