java-io

What is the buffer size in BufferedReader?

故事扮演 提交于 2019-11-27 19:23:57
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.out.println("Enter the data to be read by the bufferedReader: "); //here isr is containing the lnefeed

BufferedReader vs Console vs Scanner

◇◆丶佛笑我妖孽 提交于 2019-11-27 17:17:54
Hi I'm new to Java and I would like to know what is the best choice to read a user Input in the console, as far as I know there are 3 ways to do it: Console console = System.console(); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Scanner reader = new Scanner(System.in); Which one should I choose? Why that one and not the other ones? tom BufferedReader Since Java 1.1 Throws checked exceptions Can read chars, char arrays, and lines Fast Scanner Since Java 1.5 Doesn't throw checked exceptions Can read lines, whitespace-delimited tokens, regex-delimited tokens, and

Scanner on text file hasNext() is infinite

六眼飞鱼酱① 提交于 2019-11-27 15:43:42
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(file); int count = 0; while (inFile.hasNext()) count++; int[] numbersArray = new int[count]; inFile

how to create new java.io.File in memory? [closed]

最后都变了- 提交于 2019-11-27 12:58:01
How can I create new File (from java.io) in memory - not on the hard disk? I am using the Java language. I don't want to save the file on the hard drive. I'm faced with bad API (java.util.jar.JarFile). It's expecting File file of String filename. I have no file (only byte[] content) and can create temporary file, but it's not beautiful solution. I need to validate digest of signed jar. byte[] content = getContent(); File tempFile = File.createTempFile("tmp", ".tmp"); FileOutputStream fos = new FileOutputStream(tempFile); fos.write(archiveContent); JarFile jarFile = new JarFile(tempFile);

Java difference between FileWriter and BufferedWriter

女生的网名这么多〃 提交于 2019-11-27 10:39:08
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 the second first buffers it to the memory and writes it once? BufferedWriter is more efficient if you

Using java.io library in eclipse so FileInputStream can read a dat file

こ雲淡風輕ζ 提交于 2019-11-27 09:49:45
Goal : Print the data from a .dat file to the console using Eclipse. (Long-Term Goal) : Executable that I can pass a .dat file to and it creates a new txt file with the data formatted. The .dat : I know the .dat file contains control points that I will need to create a graph with using ECMAScript. Eclipse Setup: Created Java Project New > Class .. called the Class FileRead Now I have FileRead.java which is: 1/ package frp; 2/ 3/ import java.io.BufferedReader; 4/ import java.io.File; 5/ import java.io.FileReader; 6/ 7/ public class FileRead { 8/ 9/ public static void main(String[] args) { 10/

Java I/O streams; what are the differences?

痴心易碎 提交于 2019-11-27 09:41:51
问题 java.io has many different I/O streams, (FileInputStream, FileOutputStream, FileReader, FileWriter, BufferedStreams... etc.) and I am confused in determining the differences between them. What are some examples where one stream type is preferred over another, and what are the real differences between them? 回答1: This is a big topic! I would recommend that you begin by reading I/O Streams: An I/O Stream represents an input source or an output destination. A stream can represent many different

java IO to copy one File to another

柔情痞子 提交于 2019-11-27 08:53:12
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 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(src, 0, src.size()); And don't forget to handle exceptions and close everything in a finally block. Maddy If

java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute on Windows

本秂侑毒 提交于 2019-11-27 07:36:07
问题 I am using Java 7 File API. I wrote a class that is working fine on Ubuntu creating directories perfectly, but when I run same code on Windows then it is throwing error: Exception in thread "main" java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute at sun.nio.fs.WindowsSecurityDescriptor.fromAttribute(Unknown Source) at sun.nio.fs.WindowsFileSystemProvider.createDirectory(Unknown Source) at java.nio.file.Files.createDirectory(Unknown Source) at java

Reading file part-by-part at client side and sending it via sockets and printing it part-by-part

南笙酒味 提交于 2019-11-27 07:29:26
问题 I wanted to send a part of file to server where it will be printed on server screen...however dos reads entire input...kindly suggest what i can do....is there any other way to read stream from socket into parts and copy those parts in file or print tem on screen Server side: /*Aim:to read file in parts...send part to server...write part in the file..*/ import java.io.*; import java.net.*; public class Tser { public static void main(String a[])throws IOException{ ServerSocket sock=new