My code works fine when i am uploading plain text file, because i use InputStreamRead er to read the line without giving much effort ;)
What i thought that when i store the file content, it will be in String and append the newLine(OS dependant-System.getPrope rty("line.depar ator")) after each line then i convert it into bytes(String.ge tBytes()). And my concept was, if i convert it into bytes then what would be the problem? But in case of binary file it behaves something wrong(i am not getting the actual file content). Now my question is how conversion goes on .... below i am posting two interesting code ..please explain what happens here..
In first case the image file can't be opened but in second case file opens but .. it's hazy ...
What i thought that when i store the file content, it will be in String and append the newLine(OS dependant-System.getPrope rty("line.depar ator")) after each line then i convert it into bytes(String.ge tBytes()). And my concept was, if i convert it into bytes then what would be the problem? But in case of binary file it behaves something wrong(i am not getting the actual file content). Now my question is how conversion goes on .... below i am posting two interesting code ..please explain what happens here..
Code:
String newLine = System.getProperty("line.separator");
BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream("d:/me.jpg")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:/test.jpg"));
String line = null;
StringBuilder content = new StringBuilder();
while((line = bis.readLine()) != null){
content.append(line + newLine);
}
bos.write(content.substring(0,content.length()-newLine.length()).getBytes());
bis.close();
bos.close();
Code:
String newLine = System.getProperty("line.separator");
BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream("d:/me.jpg")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:/test.jpg"));
char char_buf[] = new char[512];
int len = 0;
while((len = bis.read(char_buf)) != -1){
String content = new String(char_buf,0,len);
bos.write(content.getBytes());
}
bis.close();
bos.close();