bufferedreader

Most Robust way of reading a file or stream using Java (to prevent DoS attacks)

佐手、 提交于 2019-11-27 04:14:43
问题 Currently I have the below code for reading an InputStream . I am storing the whole file into a StringBuilder variable and processing this string afterwards. public static String getContentFromInputStream(InputStream inputStream) // public static String getContentFromInputStream(InputStream inputStream, // int maxLineSize, int maxFileSize) { StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String

Read and Write Text in ANSI format

元气小坏坏 提交于 2019-11-27 03:57:32
Please have a look at the following code import java.io.*; public class CSVConverter { private File csvFile; private BufferedReader reader; private StringBuffer strBuffer; private BufferedWriter writer; int startNumber = 0; private String strString[]; public CSVConverter(String location, int startNumber) { csvFile = new File(location); strBuffer = new StringBuffer(""); this.startNumber = startNumber; //Read try { reader = new BufferedReader(new FileReader(csvFile)); String line = ""; while((line=reader.readLine())!=null) { String[] array = line.split(","); String inputQuery = "insertQuery["

BufferedReader readLine() blocks

与世无争的帅哥 提交于 2019-11-27 03:29:04
问题 When receiving data using readLine(), even though I put a "\n" at the end of the message using the .flush when sending the message, the while loop that reads my message still blocks. Only when closing the socket connection, it leaves the loop. Here's the client code : bos = new BufferedOutputStream(socket. getOutputStream()); bis = new BufferedInputStream(socket. getInputStream()); osw = new OutputStreamWriter(bos, "UTF-8"); osw.write(REG_CMD + "\n"); osw.flush(); isr = new InputStreamReader

in what way is java.net.Socket threadsafe?

我只是一个虾纸丫 提交于 2019-11-27 03:25:44
问题 I have a Socket that I am both reading and writing to, via BufferedReaders and BufferedWriters. I'm not sure which operations are okay to do from separate threads. I would guess that writing to the socket from two different threads at the same time is a bad idea. Same with reading off the socket from two different threads at the same time. What about reading on one thread while writing on another? I ask because I want to have one thread blocked for a long time on a read as it waits for more

Using BufferedReader.readLine() in a while loop properly

淺唱寂寞╮ 提交于 2019-11-27 01:44:22
So I'm having an issue reading a text file into my program. Here is the code: try{ InputStream fis=new FileInputStream(targetsFile); BufferedReader br=new BufferedReader(new InputStreamReader(fis)); //while(br.readLine()!=null){ for(int i=0;i<100;i++){ String[] words=br.readLine().split(" "); int targetX=Integer.parseInt(words[0]); int targetY=Integer.parseInt(words[1]); int targetW=Integer.parseInt(words[2]); int targetH=Integer.parseInt(words[3]); int targetHits=Integer.parseInt(words[4]); Target a=new Target(targetX, targetY, targetW, targetH, targetHits); targets.add(a); } br.close(); }

Fastest way to read a file line by line with 2 sets of Strings on each line?

拟墨画扇 提交于 2019-11-27 01:37:31
问题 What is the fastest way I can read line by line with each line containing two Strings. An example input file would be: Fastest, Way To, Read One, File Line, By Line .... can be a large file There are always two sets of strings on each line that I need even if there are spaces between the String e.g. "By Line" Currently I am using FileReader a = new FileReader(file); BufferedReader br = new BufferedReader(a); String line; line = br.readLine(); long b = System.currentTimeMillis(); while(line !=

Java - Read all .txt files in folder

余生长醉 提交于 2019-11-27 01:20:33
问题 Let's say, I have a folder called maps and inside maps I have map1.txt , map2.txt, and map3.txt . How can I use Java and the BufferReader to read all of the .txt files in folder maps (if it is at all possible)? 回答1: Something like the following should get you going, note that I use apache commons FileUtils instead of messing with buffers and streams for simplicity... File folder = new File("/path/to/files"); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++)

Convert InputStream to BufferedReader

走远了吗. 提交于 2019-11-26 23:58:42
问题 I'm trying to read a text file line by line using InputStream from the assets directory in Android. I want to convert the InputStream to a BufferedReader to be able to use the readLine(). I have the following code: InputStream is; is = myContext.getAssets().open ("file.txt"); BufferedReader br = new BufferedReader (is); The third line drops the following error: Multiple markers at this line The constructor BufferedReader (InputStream) is undefinded. What I'm trying to do in C++ would be

What exactly does “Stream” and “Buffer” mean in Java I/O?

南楼画角 提交于 2019-11-26 23:50:03
问题 I just learned about input/output using BufferedReader . I wanted to know what exactly are the meanings of the term Stream and Buffer ? Also what does this line of code serves us: BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 回答1: Java has two kinds of classes for input and output (I/O): streams and readers/writers . Streams ( InputStream , OutputStream and everything that extends these) are for reading and writing binary data from files, the network, or whatever

Java variables not initialized error

本秂侑毒 提交于 2019-11-26 23:43:07
问题 I am getting the following error in my Java program: Java variables not initialized error... error : variable nam and r not initialized location class child But nan and r already initialized, yet I am still getting the same error. public class cla { int rn; String name; void get(String a, int x) { name = a; rn = x; } void display() { System.out.println("student name: " + name + "roll no.:" + rn); } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;