java-io

Create a directory if it does not exist and then create the files in that directory as well

家住魔仙堡 提交于 2019-12-17 22:29:45
问题 Condition is if directory exists it has to create files in that specific directory with out creating a new directory. The below code only creating a file with new directory but not for existing directory . For example the directory name would be like "GETDIRECTION" String PATH = "/remote/dir/server/"; String fileName = PATH.append(id).concat(getTimeStamp()).append(".txt"); String directoryName = PATH.append(this.getClassName()); File file = new File(String.valueOf(fileName)); File directory =

ClassCastException when Appending Object OutputStream

一个人想着一个人 提交于 2019-12-17 18:59:14
问题 I have been trying to do a little project that needs an appendable ObjectOutputStream. I have gone through a couple of solutions and i found this It seemed to solve my problem at first. But on further development of my project i started getting unexpected exceptions. The following are my classes. public class PPAccount implements Serializable { private Profile profile; private String email; private float accountBal; private boolean isActivated; private String activationCode; private ArrayList

java IO to copy one File to another

不羁岁月 提交于 2019-12-17 09:48:03
问题 I have two Java.io.File objects file1 and file2. I want to copy the contents from file1 to file2. Is there an standard way to do this without me having to create a method that reads file1 and write to file2 回答1: No, there is no built-in method to do that. The closest to what you want to accomplish is the transferFrom method from FileOutputStream , like so: FileChannel src = new FileInputStream(file1).getChannel(); FileChannel dest = new FileOutputStream(file2).getChannel(); dest.transferFrom

Getting java.net.SocketTimeoutException: Connection timed out in android

自古美人都是妖i 提交于 2019-12-17 03:01:14
问题 I'm relatively new to android development. I'm developing an android application where i'm sending request to web server and parsing json objects. Frequently i'm getting java.net.SocketTimeoutException: Connection timed out exception while communicating with the server. Some times it will work perfectly without any problem. I know this same question has been asked in SO many times. But still i didn't get any satisfying solution to this problem. I'm posting my logcat and app-server

Streaming large files in a java servlet

那年仲夏 提交于 2019-12-17 02:28:37
问题 I am building a java server that needs to scale. One of the servlets will be serving images stored in Amazon S3. Recently under load, I ran out of memory in my VM and it was after I added the code to serve the images so I'm pretty sure that streaming larger servlet responses is causing my troubles. My question is : is there any best practice in how to code a java servlet to stream a large (>200k) response back to a browser when read from a database or other cloud storage? I've considered

Two streams and one file

本秂侑毒 提交于 2019-12-14 03:36:31
问题 What will happen if I create two instances of class FileInputStream and FileOutputStream using the default constructor and as an argument specify the same path and file name like this.. FileInputStream is = new FileInputStream("SomePath/file.txt"); FileOutputStream os = new FileOutputStream("SamePath/file.txt"); Let's imagine that we have a few strings inside the file "file.txt". Next, using a loop I am trying to read bytes from the file.txt and write them into the same file.txt each

Issue with Base64 encoding/decoding: decoded string is '?'

偶尔善良 提交于 2019-12-14 01:53:45
问题 I am trying to read an image and use Base64 encoding to convert it into byte array and then to string to send it over network. The problem is that when I try to decode the Base64 encoded string, I am getting incorrect data. For eg. I am facing issue with below special character. I am using following code for encoding: byte[] b = Base64.encodeBase64(IOUtils.toByteArray(loInputStream)); String ab = new String(b); IOUtils is org.apache.commons.io.IOUtils . and loInput Code for decoding: byte[] c

Cannot implicitly convert type 'System.IO.Stream' to 'Java.IO.InputStream'

人走茶凉 提交于 2019-12-13 13:07:55
问题 I referred some similar questions on SO but none of them deals with IO . I had used the same code in java when I used Eclipse . That time it worked. But now I try to use this code in Mono for Android (C#), it doesn't work. I'm trying to run this code to create an InputStream : InputStream myInput =ctx.Assets.Open(DATABASE_NAME + ".db"); But it is giving me compile-time error : Cannot implicitly convert type 'System.IO.Stream' to 'Java.IO.InputStream' There is a direct function to copy a file

java.io.StreamCorruptedException: invalid stream header: 4D6F7374

廉价感情. 提交于 2019-12-13 09:20:43
问题 The Java code is ObjectInputStream ois=new ObjectInputStream(new FileInputStream("src/Stringdata.txt")); String s=(String)ois.readObject(); System.out.println(s.toString()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } here am getting java.io.StreamCorruptedException: invalid stream header: 4D6F7374 this error please help me 回答1: An ObjectInputStream deserializes primitive data and objects previously written using an

How to specify which file to write to?

被刻印的时光 ゝ 提交于 2019-12-13 08:25:15
问题 I'm creating a program that takes the users input of currentSales, and if it's less than 1000 it goes to lowPerformer or more than 1000 goes to highPerformer.txt file. I'm stuck at the if statement. I'm not sure how I would code the program to tell the data what file to save to. Any assistance is appreciated. public class HighandLowSales { public static void main(String[] args) { Scanner input1 = new Scanner(System.in); Path highPerformer = Paths.get("C:\\Users\\C\\Desktop\\IS103 " +