bufferedreader

Java: How read a File line by line by ignoring “\n”

99封情书 提交于 2019-12-04 09:40:07
问题 I'm trying to read a tab separated text file line per line. The lines are separated by using carriage return ("\r\n") and LineFeed (\"n") is allowed within in tab separated text fields. Since I want to read the File Line per Line, I want my programm to ignore a standalone "\n". Unfortunately, BufferedReader uses both possibilities to separate the lines. How can I modify my code, in order to ignore the standalone "\n"? try { BufferedReader in = new BufferedReader(new FileReader(flatFile));

Understanding how BufferedReader works in Java

夙愿已清 提交于 2019-12-04 07:10:12
Very basic question on how BufferedReader works. Given the string/phrase, I want to find and print it from the file with a lot of text in it. using BufferedReader in Java I did some research on this topic and that was the closest result. Not quite addressing my problem though. So with this information, why does the following code terminate? public class MainApp { String line = null; String phrase = "eye"; try { File file = new File("text.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while((line = br.readLine()) != null) { if (line.equals(phrase) {

How to read files with an offset from Hadoop using Java

一个人想着一个人 提交于 2019-12-04 05:17:10
Problem: I want to read a section of a file from HDFS and return it, such as lines 101-120 from a file of 1000 lines. I don't want to use seek because I have read that it is expensive. I have log files which I am using PIG to process down into meaningful sets of data. I've been writing an API to return the data for consumption and display by a front end. Those processed data sets can be large enough that I don't want to read the entire file out of Hadoop in one slurp to save wire time and bandwidth. (Let's say 5 - 10MB) Currently I am using a BufferedReader to return small summary files which

How to read array of integers from the standard input in Java?

烈酒焚心 提交于 2019-12-04 02:32:11
问题 in one line from the standard input I have 3 types of integers: the first integer is id, the second integer is N - some number, and after that follows N integers, separeted by a single space which I want to store in array or ArrayList. How can I do this using BufferedReader? I have the following code: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] line = br.readLine().split(" "); int ID = Integer.parseInt(line[0]); int N = Integer.parseInt(line[1]); My

why does this reader read off strange bits of data?

泄露秘密 提交于 2019-12-04 02:11:36
问题 I'm trying to read a text file, i'm using fileImputStream, and reading all the lines into a single String then outputing it into the console (System.out) When I try to read the humanSerf.txt, it gives me this in the consol: {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0 \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535

Newline character omitted while reading from buffer

断了今生、忘了曾经 提交于 2019-12-04 00:42:25
问题 I've written the following code: public class WriteToCharBuffer { public static void main(String[] args) { String text = "This is the data to write in buffer!\nThis is the second line\nThis is the third line"; OutputStream buffer = writeToCharBuffer(text); readFromCharBuffer(buffer); } public static OutputStream writeToCharBuffer(String dataToWrite){ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new

How is the best way to extract the entire content from a BufferedReader object in Java?

人走茶凉 提交于 2019-12-03 16:07:47
i'm trying to get an entire WebPage through a URLConnection. What's the most efficient way to do this? I'm doing this already: URL url = new URL("http://www.google.com/"); URLConnection connection; connection = url.openConnection(); InputStream in = connection.getInputStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(in)); StringBuffer html = new StringBuffer(); String line = bf.readLine(); while(line!=null){ html.append(line); line = bf.readLine(); } bf.close(); html has the entire HTML page. Your approach looks pretty good, however you can make it somewhat more efficient

buffered reader not receiving data from socket

白昼怎懂夜的黑 提交于 2019-12-03 16:05:08
I am writing a client application that will receive a continuous flow of data through tcp/ip. The problem I'm having is that the buffered reader object isn't receiving any data and is hanging at the readline method. The way the server works is that you connect to it, and then send authentication information in order to receive data. The gist of my code is below socket = new Socket(strHost, port); authenticate(); inStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); process(inStream); authenticate() { PrintWriter pwriter = new PrintWriter(socket.getOutputStream(), true)

SSLHandShakeException No Appropriate Protocol

最后都变了- 提交于 2019-12-03 14:51:47
I recently added SSL to my website and it can be accessed over https. Now when my java application tries to make requests to my website and read from it with a buffered reader it produces this stack trace Im not using a self signed certificate the cert is from Namecheap who uses COMODO SSL as the CA to sign my certificate. im using java 8 javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at sun.security.ssl.Handshaker.activate(Handshaker.java:503) at sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1482)

Should I buffer the InputStream or the InputStreamReader?

梦想与她 提交于 2019-12-03 10:43:16
问题 What are the differences (if any) between the following two buffering approaches? Reader r1 = new BufferedReader(new InputStreamReader(in, "UTF-8"), bufferSize); Reader r2 = new InputStreamReader(new BufferedInputStream(in, bufferSize), "UTF-8"); 回答1: r1 is more efficient. The InputStreamReader itself doesn't have a large buffer. The BufferedReader can be set to have a larger buffer than InputStreamReader . The InputStreamReader in r2 would act as a bottleneck. In a nut: you should read the