public class BufferedReaderDemo {
public static void main(String[] args) throws Exception {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
// open input stream test.txt for reading purpose.
is = new FileInputStream("c:/test.txt");
// create new input stream reader
isr = new InputStreamReader(is);
// create new buffered reader
br = new BufferedReader(isr);
int value = 0;
// reads to the end of the stream
while((value = br.read()) != -1) {
// converts int to character
char c = (char)value;
// prints character
System.out.println(c);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
// releases resources associated with the streams
if(is!=null)
is.close();
if(isr!=null)
isr.close();
if(br!=null)
br.close();
}
}
}
public class BufferedReaderReadMethod {
public static void main(String[] args) {
System.out.println("Do you want to Continue? ");
// declare the BufferedReader Class
// used the InputStreamReader to read the console input
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
int readVal;
// catch the possible IOException by the read() method
try {
// assign the return value of the read() method to a variable
readVal = reader.read();
// print the read char converted in int
System.out.println("Character from console in int:" + readVal);
// print the char read from console input
// by the BufferedReader class
System.out.println("Character from console in char:"
+ (char) readVal);
// close the BufferedReader object
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class App {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("C:\\technicalkeeda\\hello.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int i = 0;
while ((i = bufferedReader.read()) != -1) {
char c = (char) i;
System.out.print(c + " ");
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String readAllCharsOneByOne(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
int value;
while ((value = reader.read()) != -1) {
content.append((char) value);
}
return content.toString();
}
public class Main {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("c:/test.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
char[] cbuf = new char[is.available()];
br.read(cbuf, 2, 10);
for (char c : cbuf) {
if (c == (char) 0) {
c = '*';
}
// prints characters
System.out.print(c);
}
}
}
public void testOpenBufferedStream() throws IOException {
BufferedReader reader = source.openBufferedStream();
assertTrue(source.wasStreamOpened());
assertFalse(source.wasStreamClosed());
StringWriter writer = new StringWriter();
char[] buf = new char[64];
int read;
while ((read = reader.read(buf)) != -1) {
writer.write(buf, 0, read);
}
reader.close();
writer.close();
assertTrue(source.wasStreamClosed());
assertEquals(STRING, writer.toString());
}
public void writeBinaryStream(java.io.InputStream x) throws SQLException {
BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));
try {
int i;
while( (i=bufReader.read()) != -1 ) {
char ch = (char)i;
StringBuffer strBuf = new StringBuffer();
strBuf.append(ch);
String str = new String(strBuf);
String strLine = bufReader.readLine();
writeString(str.concat(strLine));
}
} catch(IOException ioe) {
throw new SQLException(ioe.getMessage());
}
}
private static String readFileToString(String filePath) {
StringBuilder fileData = new StringBuilder(1000);
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[10];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
}
catch(IOException e) {
e.printStackTrace();
}
return fileData.toString();
}
public static String getReaderString(BufferedReader reader) {
StringBuffer out = new StringBuffer();
char[] c = new char[8192];
try {
for (int n; (n = reader.read(c, 0, c.length)) != -1;) {
out.append(c, 0, n);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return out.toString();
}
private String consumeResponse(InputStream inputStr, Charset encoding) {
StringBuilder body = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStr, encoding));
try {
int i;
char[] cbuf = new char[BUFFER_SIZE];
while ((i = reader.read(cbuf)) > 0) {
if (body == null) {
continue;
}
if (body.length() + i >= MAX_RESP_LENGTH) {
body = null;
} else {
body.append(cbuf, 0, i);
}
}
} catch (IOException e) {
LOGGER.error("Error reading response", e);
}
return body == null ? "Response too long" : body.toString();
}