Tag: fileoutputstream

使用ObjectOutputStream写入文件而不覆盖旧数据

我需要在eclipse上的文件上编写字符串而不覆盖旧字符串。 该函数类似于:创建一个字符串,将其保存在文件中,创建另一个字符串,将其保存在文件中,这包含多个字符串。 字符串具有下一种格式: String one = name surname surname; value1 value2 value3; code 所以方法将是:创建字符串,将其保存在文件中。 创建另一个字符串,将其保存在文件上等。然后在文件上保存所需的字符串数量后,我需要读取列出控制台上所有字符串的文件。 但是现在,我只能在文件上保存一个字符串然后列出它。 如果我保存两个字符串,第二个会覆盖第一个字符串,无论如何,它不正确,因为当我想列出它们时返回null值。 这是在文件上写入字符串的方法: public void writeSelling(List wordList) throws IOException { fileOutPutStream = new FileOutputStream (file); write= new ObjectOutputStream (fileOutPutStream); for (String s : wordList){ write.writeObject(s); } write.close(); } 这就是我在主类上调用write方法的方法 : List objectlist= new ArrayList(); objectlist.add(product); //Product is the string I […]

在android中读取二进制文件

我创建了一个Task类型的自定义对象,我想将它保存在内部存储的二进制文件中。 这是我创建的类: public class Task{ private String title; private int year; private int month; private int day; private int hour; private int minute; public Task(String inputTitle, int inputYear, int inputMonth, int inputDay, int inputHour, int inputMinute){ this.title = inputTitle; this.year = inputYear; this.month = inputMonth; this.day = inputDay; this.hour = inputHour; this.minute = inputMinute; } […]

如何从txt删除行?

我的意思是,我想从android上的文本中删除一行。 我怎么删除? 我不想读取一个txt并使用删除行创建另一个。 我想删除现有txt中的行。 谢谢。

序列化之间的区别是什么,只是将对象存储在磁盘上?

我对此感到困惑。 从执行Serializable类开始,我们需要使用类FileOutputStream , ObjectOutputStream等类。 那么为什么我们不只是使用这些类来做一些事情,比如将对象输出到文件并从文件中输入对象以直接维护对象的状态? 我们为什么要首先实现Serializable然后做同样的事情?

关闭使用FileOutputStream创建的文件,以进行下一次删除

我目前在Java代码中遇到了FileOutputStream的问题。 实际上我使用FileOutputStream来创建文件,但是一旦创建了文件,就无法删除它。 据我所知,这可能来自FileOutputstream未关闭的事实。 在我的汇总代码下面: outFile = new FileOutputStream(dir+”\\”+fileName); outFile.write(“Test”); outFile.flush(); outFile.close(); outFile = null; System.gc(); 然后就没有办法删除文件,即使是“手动”。 当我的程序启动时,我无法通过简单的del删除它在Windows上。 我还尝试删除文件夹目录的内容,但它也没有用,使用此代码: static public void delDir( String place ) { File path = new File( place ); System.out.println(path.exists());//return true if( path.exists() ) { File[] f = path.listFiles(); for( int j = 0 ; j < f.length ; j++ ) […]

在Java中从FileOutputStream创建和写入文件

好的,所以我正在开发一个项目,我使用Java程序在两个类( FileSender和FileReceiver )之间启动套接字连接。 我的基本想法是FileSender看起来像这样: try { writer = new DataOutputStream(connect.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //While we have bytes to send while(filein.available() >0){ //We write them out to our buffer writer.write(filein.read(outBuffer)); writer.flush(); } //Then close our filein filein.close(); //And then our socket; connect.close(); } catch (IOException e) { […]

FileOutputStream:“close”方法是否也调用“flush”?

我真的很困惑flush和close方法。在我的代码中,我总是关闭我的FileOutputStream对象。 但我想知道如果我必须在这里使用flush方法,我在哪里可以使用它? 我将编写一个重复下载4或5个文件的项目。 我将编写一个方法(用于下载文件),我的方法将循环并重复下载文件。我的方法将有这样的代码。 close方法是否调用flush ,或者在关闭之前是否必须使用flush? try { InputStream inputStream = con.getInputStream(); FileOutputStream outputStream = new FileOutputStream(“C:\\programs\\TRYFILE.csv”); int bytesRead = -1; byte[] buffer = new byte[4096]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch(Exception e) { // } finally { outputStream.close(); inputStream.close(); } 请注意,代码运行良好:它成功下载文件。 但我不确定使用flush 。

下载图像时,FileOutputStream因“打开失败:EISDIR(是一个目录)”错误而崩溃

我正在尝试从互联网上下载iamge,这是代码: try { String imgURL = c.imgURL; String imgPATH = c.imgPATH; URL url = new URL(imgURL); URLConnection conexion = url.openConnection(); conexion.connect(); int lenghtOfFile = conexion.getContentLength(); try { File f = new File(imgPATH); f.mkdirs(); BufferedInputStream input = new BufferedInputStream(url.openStream()); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(imgPATH), 8192); // CRASH HERE byte data[] = new byte[8192]; long total […]

如何在不覆盖整个文件的情况下覆盖.properties中的一个属性?

基本上,我必须通过Java应用程序覆盖.properties文件中的某个属性,但是当我使用Properties.setProperty()和Properties.Store()时,它会覆盖整个文件而不是仅覆盖那个属性。 我尝试使用append = true构建FileOutputStream,但是它添加了另一个属性,并且不会删除/覆盖现有属性。 我如何对其进行编码,以便设置一个属性会覆盖该特定属性,而不会覆盖整个文件? 编辑:我尝试读取文件并添加到它。 这是我更新的代码: FileOutputStream out = new FileOutputStream(“file.properties”); FileInputStream in = new FileInputStream(“file.properties”); Properties props = new Properties(); props.load(in); in.close(); props.setProperty(“somekey”, “somevalue”); props.store(out, null); out.close();

写入控制台和文本文件

我从互联网上找到了下面的代码,但它不会将打印的控制台写入omt.txt,它只会在第二个catch块之后写入System.out.println语句。如果你运行代码,你会明白什么我的意思是。我想要的是将控制台上的内容写入“omt.txt”文件,这些文件都是…… 经过一些回答,我发现我的问题不明确,对不起。 我想将控制台输出保存到omt.txt文本文件中。 如果在控制台上打印“Hello 123”,它也应该在omt.txt文件中。换句话说,打印机上的任何内容都应该同时写在om.txt文件中,或者可以在控制台执行后但是应该是1对1相同! import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; public class Wrt_file { public static void main(String[] args) { System.out.println(“THIS is what I see on the console. but not on TEXT file”); File f = new File(“omt.txt”); if(!f.exists()) { try { f.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } try { FileOutputStream […]