java-io

Sending image as text in java

◇◆丶佛笑我妖孽 提交于 2019-12-23 18:40:10
问题 Is it possible to read image as text and send it over network? Is yes, then how can we do this? 回答1: You could base64 encode the image to produce a (text) string. Apache Commons Codec has a Base64 implementation that you can easily use: import org.apache.commons.codec.binary.Base64; // Read the byte array from file, DB, etc byte[] imageByteArray = getImageByteArray(); String base64Image = Base64.encodeBase64String(imageByteArray); 来源: https://stackoverflow.com/questions/6484002/sending-image

FileOutputstream.close() is not always writing bytes to file system?

拈花ヽ惹草 提交于 2019-12-23 12:57:23
问题 After reading this answer from @Peter Lawrey and especially this sentence : close() can ensure the file is actually written to disk ( or not depending on the OS ) (emphasis is mine.) I have 3 questions : Is it true that there is no guaranty that all the bytes will be available on disk after calling close() ? (I guess it's true since it came from @Peter Lawrey) In general (i.e. valid on all OS), what is the best way to be sure that all bytes are effectively written to disk ? (I can imagine

DataOutputStream#writeBytes(String) vs BufferedWriter#write(String)

社会主义新天地 提交于 2019-12-23 09:46:46
问题 I would like to create a HTML file for my report. The content in the report can be created either by using BufferedWriter#write(String) File f = new File("source.htm"); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); bw.write("Content"); or by using DataOutputStream#writeBytes(String) File f = new File("source.htm"); DataOutputStream dosReport = new DataOutputStream(new FileOutputStream(f)); dosReport.wrtiteBytes("Content"); Is one of them better than the other? Why is it so? 回答1:

How to save file in memory and read file output stream?

亡梦爱人 提交于 2019-12-23 04:47:13
问题 When I save file I use: File file = new File(filename); But, since I no longer have privileges to write to folders, I would like rather to save it to memory and then read file to FileOutputStream. I have read I can save file to memory with this approach: new ByteArrayOutputStream(); How would whole code look like? I can't figure it out how to write it properly after upload is done. Edit: I'm using Vaadins upload plugin: public File file; public OutputStream receiveUpload(String filename,

How do I move zip file to blob column in Java?

我们两清 提交于 2019-12-23 02:52:57
问题 I have a Java program that creates a number of xml file and then zips them up and saves them to the file system. Later in the program I want to put that same zip file into a blob column of my oracle database. Problem is I'm not sure how to do that. I don't need to read it or do anything with the data, just move it to the database for persistent, central storage. Thanks! 回答1: There are several ways you can do this, but PreparedStatement.setBinaryStream is probably the best way. public void

Deleting a Record Using Random Access File

杀马特。学长 韩版系。学妹 提交于 2019-12-23 02:38:45
问题 This question is related to thread Address Book Functionality which was closed considering being too broad. I was developing a small test application using Random Access File. I am attaching the source code with it. I want to delete a record from the file. I have searched similar threads on stackoverflow Delete Record Random Access File.These threads suggest setting a flag as deleted against a record and not display it. I want to actually delete it. I tried to search the record and tried to

Inappropriate ioctl for device from FileOutputStream.close()

跟風遠走 提交于 2019-12-22 10:46:42
问题 I have some code that saves some preferences to file using FileOutputStream . It is standard code I've written a thousand times: FileOutputStream out = new FileOutputStream(file); try { BufferedOutputStream bos = new BufferedOutputStream(out); try { store(bos, comments); } finally { bos.close(); } } finally { out.close(); } One of our users is reporting the following error on Linux during the close() call. java.io.IOException: Inappropriate ioctl for device at java.io.FileOutputStream.close0

About File file = new File(path)

﹥>﹥吖頭↗ 提交于 2019-12-22 09:06:53
问题 The Java.iO.File document says the following words about its constructor which takes the pathname : public File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname. But what if the pathname points to a file which is already existing ? File file = new File(PATH_TO_AN_EXISTING_FILE); Does the above file instance represent a fresh new file (with the

Why is my program denying access to create a file?

末鹿安然 提交于 2019-12-22 08:23:33
问题 In the book I'm reading on Java, it demonstrates serialization by using a program that writes to a file and stores it away. I'm getting a strange error that I don't know how to read and it denies me access to creating a .txt file. Here's the error: Exception in thread "main" java.io.FileNotFoundException: C:\testFile.txt (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at

java.nio.file.FileAlreadyExistsException how to resolve this in java7

纵然是瞬间 提交于 2019-12-22 08:14:39
问题 i am writing a code i am creating a directory with java nio api my segment of code is Path target = Paths.get(""+folder_path+xx[0]); Set<PosixFilePermission> perms = null; if(xx[2].toLowerCase().equals("read")) perms =PosixFilePermissions.fromString("r--------"); if(xx[2].toLowerCase().equals("read/write")) { perms =PosixFilePermissions.fromString("rw-------"); } FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); Files.createDirectory(target, attr);