当前位置:首页 » 文件管理 » 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的

~~~~~~~

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:708
制作脚本网站 发布:2025-10-20 08:17:34 浏览:971
python中的init方法 发布:2025-10-20 08:17:33 浏览:680
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:832
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:739
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1079
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:310
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:190
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:877
python股票数据获取 发布:2025-10-20 07:39:44 浏览:834