java-io

What does System.in.read actually return?

谁说我不能喝 提交于 2019-11-26 20:58:53
What does : System.in.read() return ? The documentation says : Returns: the next byte of data, or -1 if the end of the stream is reached. But for example if I enter : 10 I get back 49 . Why is that ? 49 is the ASCII value of the char 1 . It is the value of the first byte. The stream of bytes that is produced when you enter 1 0 Enter on your console or terminal contains the three bytes {49,48,10} (on my Mac, may end with 10,12 or 12 instead of 10, depending on your System). So the output of the simple snippet int b = System.in.read(); while (b != -1) { System.out.println(b); b = System.in.read(

Basics - reading/writing remote files using Java

孤人 提交于 2019-11-26 20:28:48
问题 I started with requirement of reading and writing files in from/in a directory on a remote Ubuntu machine. First, I wrote a Java program that could read,write files from a shared folder on a remote Windows machine i.e on a LAN. Here, something like this works on my(local) Windows machine : File inputFile = new File( "\\172.17.89.76\EBook PDF");/*ignore the syntax errors, the loc is just for the idea*/ Now when I consider a remote Ubuntu machine, obviously I cannot do something like this as

What is the buffer size in BufferedReader?

末鹿安然 提交于 2019-11-26 19:51:44
问题 What is the sense of buffer size in the constructor? BufferedReader(Reader in, int size) As i have written the program: import java.io.*; class bufferedReaderEx{ public static void main(String args[]){ InputStreamReader isr = null; BufferedReader br = null; try{ isr = new InputStreamReader(System.in); // System.out.println("Write data: "); // int i = isr.read(); // System.out.println("Data read is: " + i); //Thus the InputStreamReader is useful for reading the character from the stream System

How to get files from resources folder. Spring Framework

百般思念 提交于 2019-11-26 18:27:56
问题 I'm trying to unmarshal my xml file: public Object convertFromXMLToObject(String xmlfile) throws IOException { FileInputStream is = null; File file = new File(String.valueOf(this.getClass().getResource("xmlToParse/companies.xml"))); try { is = new FileInputStream(file); return getUnmarshaller().unmarshal(new StreamSource(is)); } finally { if (is != null) { is.close(); } } } But I get this errors: java.io.FileNotFoundException: null (No such file or directory) Here is my structure: Why I can't

how to File.listFiles in alphabetical order?

扶醉桌前 提交于 2019-11-26 17:38:49
问题 I've got code as below: class ListPageXMLFiles implements FileFilter { @Override public boolean accept(File pathname) { DebugLog.i("ListPageXMLFiles", "pathname is " + pathname); String regex = ".*page_\\d{2}\\.xml"; if(pathname.getAbsolutePath().matches(regex)) { return true; } return false; } } public void loadPageTrees(String xml_dir_path) { ListPageXMLFiles filter_xml_files = new ListPageXMLFiles(); File XMLDirectory = new File(xml_dir_path); for(File _xml_file : XMLDirectory.listFiles

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

情到浓时终转凉″ 提交于 2019-11-26 17:23:28
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 communication code below. public JSONObject RequestWithHttpUrlConn(String _url, String param){ HttpURLConnection

Scanner on text file hasNext() is infinite

北战南征 提交于 2019-11-26 17:16:09
问题 I'm writing a simple program in Java and it requires reading data from a text file. However, I'm having trouble counting lines. The issue seems generic enough for a simple Google search but I may not even be searching the right things. The textbook I'm learning from suggests that to count the number of lines in a text file, you should do something like this: public static int[] sampleDataArray(String inputFile) throws IOException { File file = new File(inputFile); Scanner inFile = new Scanner

Process Builder waitFor() issue and Open file limitations

坚强是说给别人听的谎言 提交于 2019-11-26 17:03:16
问题 I have inherited some code: Process p = new ProcessBuilder("/bin/chmod", "777", path).start(); p.waitFor(); Basically, there is for some ancient and highly voodoo based reason for storing key/value pairs on disk as files. I don't really want to go into it. However, I am left with a bunch of IO exceptions: Exception :Cannot run program "/bin/chmod": java.io.IOException: error=24, Too many open files Message: Cannot run program "/bin/chmod": java.io.IOException: error=24, Too many open files

Java difference between FileWriter and BufferedWriter

房东的猫 提交于 2019-11-26 15:16:22
问题 What's the difference between those? I'm just learning Java ATM, but it seems like I can write to a file both ways i.e. (I didn't copy the try-catch block here.) FileWriter file = new FileWriter("foo.txt"); file.write("foobar"); file.close(); and FileWriter file = new FileWriter("foo.txt"); BufferedWriter bf = new BufferedWriter(file); bf.write("foobar"); bf.close(); I understand the concept of buffering the data first, so does that mean the first example writes the characters one by one and

Java InputStream blocking read

坚强是说给别人听的谎言 提交于 2019-11-26 15:06:48
According to the java api, the InputStream.read() is described as: If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. I have a while(true) loop doing a read and I always get -1 when nothing's sent over the stream. That's expected. My question is when would read() ever block? Since if it doesn't get any data it returns -1. I would expect a blocking read to wait until data is received. If you've reached the end of the input stream, shouldn