I must split this input from file into vectors and add to a vectors.
File input
1,375,seller
1,375,sellers
1,375,send
1,375,sister
1,375,south
1,375,specific
1,375,spoiler
1,375,stamp
1,375,state
1,375,stop
1,375,talked
1,375,tenant
1,375,today
1,375,told
FileInputStream fstream = new FileInputStream("e://inputfile.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null)
{
Vector dataPoints = new Vector();
dataPoints.add(br);
dataPoints.add(new DataPoint());
}
------ public DataPoint(double x, double y, String name) this is the method
How to split the string into double and string and give a input to the vector?
Use String#split(String):
Vector<DataPoint> dataPoints = new Vector<DataPoint>();
while ((strLine = br.readLine()) != null) {
String[] array = strLine.split(",");
dataPoints.add(new DataPoint(Double.parseDouble(array[0]), Double.parseDouble(array[1]), array[2]));
}
Just split the String returned from the Bufferedreader using String.split() using , as a delimiter. and also consider using an ArrayList instead of Vector, unless you care about thread safety and also make your collections generic.
Vector<DataPoint> dataPoints = new Vector<DataPoint>();
while ((strLine = br.readLine()) != null)
{
String[] arr = strLine.split(",");
DataPoint point = new DataPoint(Double.valueOf(arr[0]), Double.valueOf(arr[1]), arr[2]);
dataPoints.add(point);
}
Related
I am trying to extract a specific content from text file by using delimiters. This is my code :
File file = new File("C:\\Inputfiles\\message.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st=br.readLine()) != null) {
String[] strings = StringUtils.split(st, "------------");
System.out.println(strings);}
But as a result, each and everyline is getting splitted by delimiter and saved as array.
Can anyone suggest how I can save the contents from file as a single string, so I can get limited number of lines only as Array.
You can use StringBuilder or StringBuffer to do that.
File file = new File("C:\\Inputfiles\\message.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st=br.readLine()) != null) {
String[] strings = StringUtils.split(st, "------------");
StringBuilder singleString = new StringBuilder();
for(String s : strings){
singleString.append(s);
}
System.out.println(singleString.toString());
}
Thanks All,
I did using below changes
String contents = new String(Files.readAllBytes(Paths.get("C:\\Inputfiles\\message1.txt")));
String[] splitted = StringUtils.split(contents, "-------");
for(int i=0;i<splitted.length;i++)
System.out.println(splitted[i]);
ArrayList storeList = new ArrayList<USCrimeClass>;
FileInputStream fstream = new FileInputStream(inFile);
try ( // Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream)) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
strLine = br.readLine();// skip first line
while ((strLine = br.readLine()) != null) {
// lines
storeList.add(storeToCrimeObjin(strLine));
}
// Close the input stream
There are missing () in new ArrayList<USCrimeClass>; it should be:
ArrayList storeList = new ArrayList<USCrimeClass>();
ArrayList<USCrimeClass> storeList = new ArrayList<>();
The diamond operator <> is now in the latter part of the statement.
Apart from that, I'd say #gawi is right.
I'm using the code below to compare two file input streams and compare them:
java.util.Scanner scInput1 = new java.util.Scanner(InputStream1);
java.util.Scanner scInput2 = new java.util.Scanner(InputStream2);
while (scInput1 .hasNext() && scInput2.hasNext())
{
// do some stuff
// output line number of InputStream1
}
scInput1.close();
scInput2.close();
How can I output the line number inside the while loop?
Use LineNumberReader on an InputStreamReader. As it is precisely made for that sole purpose.
try (LineNumberReader scInput1 = new LineNumberReader(new InputStreamReader(
inputStream1, StandardCharsets.UTF_8));
LineNumberReader scInput2 = new LineNumberReader(new InputStreamReader(
inputStream2, StandardCharsets.UTF_8))) {
String line1 = scInput1.readLine();
String lien2 = scInput2.readLine();
while (line1 != null && line2 != null) {
...
scInput1.getLineNumber();
line1 = scInput1.readLine();
line2 = scInput2.readLine();
}
}
Here I have added the optional CharSet parameter.
A Scanner has additional tokenizing capabilities not needed.
Hi. I am trying to read a text file from a dropbox URL and put the contents of the text file to the ArrayList.
I was able to read and out print the data by using openStream() method but I can't seem to be able to figure out how to put that data into an ArrayList
URL pList = new URL("http://url");
BufferedReader in = new BufferedReader(
new InputStreamReader(
pList.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
would appreciate the help ?
List<String> list = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
list.add(inputLine);
}
Try something like this:
String inputLine;
ArrayList<String> array = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
array.add(inputLine);
}
It depends on what you want to do:
Store multiple DropBox files in an ArrayList, where 1 item represents 1 file
Use a StringBuilder to stitch all the lines together to one string.
List<String> files = new ArrayList<String>();
files.add(readFileUsingStringBuilder(pList));
public static String readFileUsingStringBuilder(URL url)
{
StringBuilder sb = new StringBuilder();
String separator = "";
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = br.readLine() != null)
{
sb.append(separator);
sb.append(line);
separator = "\n";
}
return sb.toString();
}
Store each line of the file in a record of the ArrayList
String inputLine;
ArrayList<String> array = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
array.add(inputLine);
}
I have an input text file in this format:
<target1> : <dep1> <dep2> ...
<target2> : <dep1> <dep2> ...
...
And a method that takes two parameters
function(target, dep);
I need to get this parsing to call my method with each target and dep eg:
function(target1, dep1);
function(target1, dep2);
function(target1, ...);
function(target2, dep1);
function(target2, dep2);
function(target2, ...);
What would be the most efficient way to call function(target,dep) on each line of a text file? I tried fooling around with the scanner and string.split but was unsuccessful. I'm stumped.
Thanks.
Read line into String myLine
split myLine on : into String[] array1
split array1[1] on ' ' into String[] array2
Iterate through array2 and call function(array1[0], array2[i])
So ...
FileReader input = new FileReader("myFile");
BufferedReader bufRead = new BufferedReader(input);
String myLine = null;
while ( (myLine = bufRead.readLine()) != null)
{
String[] array1 = myLine.split(":");
// check to make sure you have valid data
String[] array2 = array1[1].split(" ");
for (int i = 0; i < array2.length; i++)
function(array1[0], array2[i]);
}
The firstly you have to read line from file and after this split read line, so your code should be like:
FileInputStream fstream = new FileInputStream("your file name");
// or using Scaner
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// split string and call your function
}