java-io

when does FileInputStream.read() block?

不打扰是莪最后的温柔 提交于 2021-02-18 12:10:58
问题 The question is similar to the following two questions. Java InputStream blocking read Why is the FileInputStream read() not blocking? But I still cannot fully understand it. So far I think the read() method in following code will block due to the empty file 'test.txt'. FileInputStream fis = new FileInputStream("c:/test.txt"); System.out.println(fis.read()); System.out.println("to the end"); Actually it will print -1, I want to know why. The javadoc says This method blocks if no input is yet

How to display the contents of a .txt file on cmd window using Java?

喜夏-厌秋 提交于 2021-02-15 07:58:14
问题 I am working on a project and I want to display the contents of a .txt file on the CMD window. I wrote this piece of code to open a demo.txt file on cmd but it does not work. The "path" variable contains the location where the demo.txt file is placed (as you can see obviously). public static void main(String[] args){ try{ String path = "C:\\Users\\Hp\\Documents\\NetBeansProject\\Project\\build\\classes\\"; //cmd command to open open the txt file on cmd window String command = ("type " + path

InputStream or Reader wrapper for progress reporting

自作多情 提交于 2021-02-07 05:26:26
问题 So, I'm feeding file data to an API that takes a Reader , and I'd like a way to report progress. It seems like it should be straightforward to write a FilterInputStream implementation that wraps the FileInputStream , keeps track of the number of bytes read vs. the total file size, and fires some event (or, calls some update() method) to report fractional progress. (Alternatively, it could report absolute bytes read, and somebody else could do the math -- maybe more generally useful in the

Why I got SSLException even when I set https.protocols?

岁酱吖の 提交于 2020-04-18 05:44:13
问题 Consider a code snippet (look at first line next to main System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); ): package com.zip; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class ParallelDownload { public static class Download { public static String FILEPATH = "https://server.com/myfile.zip"; //about 60Gb public static String DESTINATION = "C:\\!deleteme

Why I got SSLException even when I set https.protocols?

我的未来我决定 提交于 2020-04-18 05:44:01
问题 Consider a code snippet (look at first line next to main System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); ): package com.zip; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class ParallelDownload { public static class Download { public static String FILEPATH = "https://server.com/myfile.zip"; //about 60Gb public static String DESTINATION = "C:\\!deleteme

java.io FileOutPutStream - There are white spaces among chars. Why?

房东的猫 提交于 2020-01-30 10:21:30
问题 i want to write something with this code but after i run there are white spaces between characters. but in code i dont give space to string. import java.io.*; public class WriteText{ public static void main(String[] args) { FileOutputStream fos; DataOutputStream dos; try { File file= new File("C:\\JavaWorks\\gui\\bin\\hakki\\out.txt"); fos = new FileOutputStream(file); dos=new DataOutputStream(fos); dos.writeChars("Hello World!"); } catch (IOException e) { e.printStackTrace(); } } } Output is

I Am Not Getting the Result I Expect Using readLine() in Java

允我心安 提交于 2020-01-29 08:43:07
问题 I am using the code snippet below, however it's not working quite as I understand it should. public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; try { line = br.readLine(); while(line != null) { System.out.println(line); line = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } } From reading the Javadoc about readLine() it says: Reads a line of text. A line is considered to be terminated by any one of

How to change a value of a file using Java IO function

泄露秘密 提交于 2020-01-24 01:16:06
问题 Can anyone help me how to change a line in a file using java IO function. The file is located at SDCard . I have tried some solution from different blog, but failed. I need to change one attribute wpa_oper_channel=1 to 2,3,4..... as user demand in the file sdcard/sample.txt . I have also tried using SED command, but still not working. Please suggest if there any solution using java IO function. The Command I have used using SED : sed -i 's/wpa_oper_channel=[0-9]\\+/wpa_oper_channel=7/' sample

ObjectInputStream from socket.getInputStream()

谁都会走 提交于 2020-01-21 10:15:48
问题 I have server ServerSocket socketListener = new ServerSocket(Config.PORT); ... client = socketListener.accept(); and client sock = new Socket("127.0.0.1", Config.PORT); I want to transfer between them some serialized data using ObjectInputStream and ObjectOutputStream. When I try to do ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); Nothing happens neither on the server side nor client side. Everything falls on that line. Both the client and the server is

ObjectInputStream from socket.getInputStream()

二次信任 提交于 2020-01-21 10:15:05
问题 I have server ServerSocket socketListener = new ServerSocket(Config.PORT); ... client = socketListener.accept(); and client sock = new Socket("127.0.0.1", Config.PORT); I want to transfer between them some serialized data using ObjectInputStream and ObjectOutputStream. When I try to do ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); Nothing happens neither on the server side nor client side. Everything falls on that line. Both the client and the server is