「java中写文件」下载文件java写法

博主:adminadmin 2022-11-22 12:41:08 58

本篇文章给大家谈谈java中写文件,以及下载文件java写法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java中对文件进行读写操作的基本类是什么?

Java.io包中包括许多类提供许多有关文件的各个方面操作。

1 输入输出抽象基类InputStream/OutputStream ,实现文件内容操作的基本功能函数read()、 write()、close()、skip()等;一般都是创建出其派生类对象(完成指定的特殊功能)来实现文件读写。在文件读写的编程过程中主要应该注意异常处理的技术。

2 FileInputStream/FileOutputStream:

用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象);

本地文件读写编程的基本过程为:

① 生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类);

② 调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容;

③ 关闭文件(close())。

3 PipedInputStream/PipedOutputStream:

用于管道输入输出(将一个程序或一个线程的输出结果直接连接到另一个程序或一个线程的输入端口,实现两者数据直接传送。操作时需要连结);

4管道的连接:

方法之一是通过构造函数直接将某一个程序的输出作为另一个程序的输入,在定义对象时指明目标管道对象

PipedInputStream pInput=new PipedInputStream();

PipedOutputStream pOutput= new PipedOutputStream(pInput);

方法之二是利用双方类中的任一个成员函数 connect()相连接

PipedInputStream pInput=new PipedInputStream();

PipedOutputStream pOutput= new PipedOutputStream();

pinput.connect(pOutput);

5 管道的输入与输出:

输出管道对象调用write()成员函数输出数据(即向管道的输入端发送数据);而输入管道对象调用read()成员函数可以读起数据(即从输出管道中获得数据)。这主要是借助系统所提供的缓冲机制来实现的。

6随机文件读写:

RandomAccessFile类(它直接继承于Object类而非InputStream/OutputStream类),从而可以实现读写文件中任何位置中的数据(只需要改变文件的读写位置的指针)。

随机文件读写编程的基本过程为:

① 生成流对象并且指明读写类型;

② 移动读写位置;

③ 读写文件内容;

④ 关闭文件。

七里河团队答疑助人,希望我的回答对你有所帮助

java写文件

public

void

writeLog(String

logFileName,

String

content)

{

try

{

/*

日志文件大小最大为100KB

*/

File

log

=

new

File(logFileName);

if

(log.length()

102400)

{

if

(log.delete())

logger.info("日志文件:

"

+

logFileName

+

"

已满,

已经自动清空.");

}

SimpleDateFormat

sdf

=

new

SimpleDateFormat("yyyy-MM-dd

hh:mm:ss

");

RandomAccessFile

raf

=

new

RandomAccessFile(logFileName,

"rw");

String

contents

=

sdf.format(new

Date())

+

content

+

"\r\n";

raf.seek(raf.length());

raf.write(contents.getBytes("GBK"));

raf.close();

}

catch

(FileNotFoundException

e)

{

e.printStackTrace();

}

catch

(IOException

e)

{

e.printStackTrace();

}

}

调用这个函数就行了,

Java写入文件

可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:OutputStreamWriterpw=null;//定义一个流pw=newOutputStreamWriter(newFileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次writepw.close();//关闭流备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

Java的文件生成然后写入

创建文件

new File(文件路径).createNewFile();这是写入文件

try{

FileOutputStream filewrite = new FileOutputStream(文件路径); //这个要捕捉异常

filewrite.write(要写入的数据byte[]);

filewrite.close();

}catch(IOException e);

java如何写入文件

package filewriter;  

  

import java.io.FileWriter;  

import java.io.IOException;  

  

public class IOExceptionDemo {  

  

    private static final String LINE_SEPARATOR = System.getProperty("line.separator");  

    public static void main(String[] args) {  

  

        FileWriter fw = null;  

        try {  

            fw = new FileWriter("k:\\Demo.txt", true);  

            fw.write("hello" + LINE_SEPARATOR + "world!");  

        } catch (Exception e) {  

            System.out.println(e.toString());  

        } finally {  

            if (fw != null)  

                try {  

                    fw.close();  

                } catch (IOException e) {  

                    throw new RuntimeException("关闭失败!");  

                }  

        }  

    }  

}

java中写文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于下载文件java写法、java中写文件的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。