fileoutputstream

Read/write file to internal private storage

我怕爱的太早我们不能终老 提交于 2019-11-27 13:28:26
问题 I'm porting the application from Symbian/iPhone to Android, part of which is saving some data into file. I used the FileOutputStream to save the file into private folder /data/data/package_name/files : FileOutputStream fos = iContext.openFileOutput( IDS_LIST_FILE_NAME, Context.MODE_PRIVATE ); fos.write( data.getBytes() ); fos.close(); Now I am looking for a way how to load them. I am using the FileInputStream , but it allows me to read the file byte by byte, which is pretty inefficient: int

How to overwrite one property in .properties without overwriting the whole file?

久未见 提交于 2019-11-27 08:50:32
Basically, I have to overwrite a certain property in a .properties file through a Java app, but when I use Properties.setProperty() and Properties.Store() it overwrites the whole file rather than just that one property. I've tried constructing the FileOutputStream with append = true, but with that it adds another property and doesn't delete/overwrite the existing property. How can I code it so that setting one property overwrites that specific property, without overwriting the whole file? Edit: I tried reading the file and adding to it. Here's my updated code: FileOutputStream out = new

Java IO 之 FileInputStream & FileOutputStream源码分析

女生的网名这么多〃 提交于 2019-11-27 03:38:17
Writer :BYSocket(泥沙砖瓦浆木匠) 微 博: BYSocket 豆 瓣: BYSocket FaceBook: BYSocket Twitter : BYSocket 一、引子 文件 ,作为常见的数据源。关于操作文件的字节流就是 — FileInputStream & FileOutputStream 。它们是 Basic IO 字节流中重要的实现类。 二、FileInputStream源码分析 FileInputStream源码如下: /** * FileInputStream 从文件系统的文件中获取输入字节流。文件取决于主机系统。 * 比如读取图片等的原始字节流。如果读取字符流,考虑使用 FiLeReader。 */ publicclassSFileInputStream extendsInputStream { /* 文件描述符类---此处用于打开文件的句柄 */ private final FileDescriptor fd; /* 引用文件的路径 */ private final String path; /* 文件通道,NIO部分 */ private FileChannel channel = null; private final Object closeLock = new Object(); private volatile boolean

Get file name from FileOutputStream

感情迁移 提交于 2019-11-27 01:56:56
Is there a way to get the file name from a FileOutputStream or from FileInputStream ? Tom G Looks like the answer is no: http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileOutputStream.html http://docs.oracle.com/javase/7/docs/api/index.html?java/io/FileOutputStream.html There are no public methods that return the File or String used in construction of the stream. EDIT: The same holds for FileInputStream . This feature is not provided by the out-of-the-box File-Input/Output-Stream, but nothing stops you from writing your own subclass that stores the File (or fileName) and provides a

Writing to console and text file

元气小坏坏 提交于 2019-11-27 01:46:32
I found the code below from the internet, works but it doesn't write the printed console to omt.txt, it only writes the System.out.println statements after the second catch block.If you run the code once you will understand what I mean.All I want is to write what is on console to the "omt.txt" file that is all... After some answers, I see that my question wasn't clear, sorry for that. I want to save console output to omt.txt text file. If on the console "Hello 123" is printed , it should be also in omt.txt file.In other words whatever on the console is printed should be simultaneously written

FileOutputStream: Does the “close” method calls also “flush”?

本小妞迷上赌 提交于 2019-11-27 01:34:55
问题 I'm really confused about flush and close method.In my code I always close my FileOutputStream object. But I want to know that if I have to use flush method here, and where can I use it? I will write a project that download 4 or 5 files repeatedly. I will write a method(for download files) and my method will be in a loop and download files repeatedly.My method will have a code like this. Does the close method calls flush , or do I have to use flush before closing? try { InputStream

FileOutputStream crashes with “open failed: EISDIR (Is a directory)” error when downloading image

纵饮孤独 提交于 2019-11-27 01:16:39
问题 I'm trying to download an iamge from the internet, Here is the code: 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

How to overwrite one property in .properties without overwriting the whole file?

烈酒焚心 提交于 2019-11-26 22:17:54
问题 Basically, I have to overwrite a certain property in a .properties file through a Java app, but when I use Properties.setProperty() and Properties.Store() it overwrites the whole file rather than just that one property. I've tried constructing the FileOutputStream with append = true, but with that it adds another property and doesn't delete/overwrite the existing property. How can I code it so that setting one property overwrites that specific property, without overwriting the whole file?

How can I let users access the internal storage directory of my app?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 16:36:14
问题 My app stores files in its internal storage directory (/Android/data/com.mycompany.myapp/files, as returned by getFilesDir()), and I would like to allow users to access those files directly from a file management app on their mobile device or the Android File Transfer desktop appplication. The Storage Options developer guide says: By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). "By default" implies

Java FileOutputStream Create File if not exists

南楼画角 提交于 2019-11-26 15:49:28
Is there a way to use FileOutputStream in a way that if a file (String filename) does not exist, then it will create it? FileOutputStream oFile = new FileOutputStream("score.txt", false); talnicolas It will throw a FileNotFoundException if the file doesn't exist and cannot be created ( doc ), but it will create it if it can. To be sure you probably should first test that the file exists before you create the FileOutputStream (and create with createNewFile() if it doesn't): File yourFile = new File("score.txt"); yourFile.createNewFile(); // if file already exists will do nothing