Get .txt file and parse it by new line or comma - java

I have a .txt file in this format:
file.txt (each line has text)
text1
text2
longtext3
...
..
I am downloding it:
URL url = new URL(FILE_URL);
URLConnection connection = url.openConnection();
connection.connect();
// download the file
InputStream input = newBufferedInputStream(connection.getInputStream());
How can I parse this input so I get the text in each new line?
I tried something like that:
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
LIST.add(line);
} }
but I don't want to save it,so I don't have the File instance
I can save in this format:
text1,text2,longtext3,....
If its more simple to extract it

You can use an InputStreamReader (http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html).
Put it between your InputStream and your BufferedReader. Now you don't need the File instance (and so there is no need to save it first).
Something like ...
InputStream input = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = br.readLine()) != null) {
LIST.add(line);
// further break the line into a list of comma separated values
List<String> commaSeparatedValues = Arrays.asList(line.split(","));
}
...

You can use the Scanner class
String result = "";
Scanner in = new Scanner(new FileInputStream(file));
while(in.hasNextLine())
result += in.nextLine() + ",";
this will create a string like this:
text1,text2,longtext3,....

I'm not sure what you're asking but I think you want to save each line of a text file. This worked for me, it puts every line into one long string.
public static void main( String[] args )
{
String FILE_URL = "http://www.google.com/robots.txt";
String FILE_CONTENTS = "";
try
{
URL url = new URL(FILE_URL);
URLConnection connection = url.openConnection();
connection.connect();
// download the file
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
while( line != null )
{
FILE_CONTENTS += line;
line = reader.readLine();
}
}
catch( MalformedURLException e )
{
System.out.println("Malformed URL" );
}
catch( IOException e )
{
System.out.println( "IOException" );
}
}

Try to scan as normal text and replace every '\n' in a comma...

Related

String index out of range: -1 working with XML file

I downloaded an xml file from web service. If I open file from file system is formed correctly, but when I run my code isn't formed correctly.
A part of xml file formed correctly, it opened from file system:
<?xml version="1.0" encoding="UTF-8"?><ns3:FatturaElettronica xmlns:ns3="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" versione="FPR12">
Here the same xml file managed by my code:
ÿþ<
I can't copy the code and I put an image of what I see on the eclipse console.
I tryed different ways to manage this file, but nothing worked.
This is the code that it manages files. I put all ways I tryed to solve the error.
private static String readFile(File file, Writer writerArg) throws FileNotFoundException, IOException,Exception
{
FileInputStream fis = null;
InputStreamReader isr = null;
String typeEncoding = null;
/*
* First way
*
* BufferedReader br = new BufferedReader(new FileReader(fileName));
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = br.readLine()) != null)
{
// System.out.println("Writing: " + nextLine);
writerArg.write(nextLine);
// sb.append(nextLine);
sb.append(nextLine+"\n");
} // Convert the content into to a string
String clobData = sb.toString().trim();
*/
/*
* Second way
*
* fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
typeEncoding = isr.getEncoding();
Charset inputCharset = Charset.forName(typeEncoding);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), inputCharset));
String str;
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = in.readLine()) != null) {
System.out.println(nextLine);
writerArg.write(nextLine);
// sb.append(nextLine);
sb.append(nextLine+"\n");
}
String clobData = sb.toString().trim();
// Return the data.
return clobData;
*/
/* Third way */
String data = "";
data = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
System.out.println(data);
return data;
}
And when the below code receives the string I get the error: "String index out of range: -1"
schema=stringXml.substring(0,stringXml.indexOf("<FatturaElettronicaHeader")).trim();
The first way downloaded thousands of files and managed them. Only this file gives my this error. It's from yesterday that I'm looking for a way to solve the error.
Can anyone give my any idea?

How can I properly read an Arabic dataset in java?

Scenario: I want to read an Arabic dataset with utf-8 encoding. Each word in each line is separated by a space.
Problem: When I read each line, the output is:
??????? ?? ???? ?? ???
Question: How can I read the file and print each line?
for more information, here is my Arabic dataset and part of my source code that reads data would be like the following:
private ContextCountsImpl extractContextCounts(Map<Integer, String> phraseMap) throws IOException {
Reader reader;
reader = new InputStreamReader(new FileInputStream(inputFile), "utf-8");
BufferedReader rdr = new BufferedReader(reader);
while (rdr.ready()) {
String line = rdr.readLine();
System.out.println(line);
List<String> phrases = splitLineInPhrases(line);
//any process on this file
}
}
I can read using UTF-8, Can you try like this.
public class ReadArabic {
public static void main(String[] args) {
try {
String line;
InputStream fileInputStream = new FileInputStream("arabic.txt");
Reader reader = new InputStreamReader(fileInputStream, "UTF-8"); // leave charset out for default
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.err.println(e.getMessage()); // handle all exceptions
}
}
}

How to append 4 digit number to the next string read from file

I have one file to read which is like this
mytxt.txt
1234 http://www.abc.com
8754 http://www.xyz.com
I tried with this
try {
// make a 'file' object
File file = new File("e:/mytxt.txt");
// Get data from this file using a file reader.
FileReader fr = new FileReader(file);
// To store the contents read via File Reader
BufferedReader br = new BufferedReader(fr);
// Read br and store a line in 'data', print data
String data;
while((data = br.readLine()) != null)
{
//data = br.readLine( );
System.out.println(data);
}
} catch(IOException e) {
System.out.println("bad !");
}
I used this but the actual question is I want to read one this two charachter one by one and then appens the digit to the link which I'll read as string.
Can anyone tell me how I am suppose to do that..?
any help would be appreciated.
Parse the line you are reading, search for the first white space (I'm assuming you have only one space separating your digit and your url) something like this:
try {
// make a 'file' object
File file = new File("e:/mytxt.txt");
// Get data from this file using a file reader.
FileReader fr = new FileReader(file);
// To store the contents read via File Reader
BufferedReader br = new BufferedReader(fr);
// Read br and store a line in 'data', print data
String data;
while((data = br.readLine()) != null)
{
int posWhite = data.indexOf(' ');
String digit = data.substring(0, posWhite);
String url = data.substring(posWhite + 1);
System.out.println(url + "/" + digit);
}
} catch(IOException e) {
System.out.println("bad !");
}
Is this what you want?
while((data = br.readLine()) != null)
{
String[] data=br.readLine().split();
if(data!=null&&data.length==2)
{
System.out.println(data[1]+"/"+data[0]);
}else
{
System.out.println("bad string!");
}
}
In the while((data = br.readLine()) != null), make the code like this:
String tmpData[] = data.split(" ");
System.out.println(tmpData[1] + "/" + tmpData[0]);

Writing multiple queries from a test file

public static void main(String[] args) {
ArrayList<String> studentTokens = new ArrayList<String>();
ArrayList<String> studentIds = new ArrayList<String>();
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(new File("file1.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF8"));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
strLine = strLine.trim();
if ((strLine.length()!=0) && (!strLine.contains("#"))) {
String[] students = strLine.split("\\s+");
studentTokens.add(students[TOKEN_COLUMN]);
studentIds.add(students[STUDENT_ID_COLUMN]);
}
}
for (int i=0; i<studentIds.size();i++) {
File file = new File("query.txt"); // The path of the textfile that will be converted to csv for upload
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while ((line = reader.readLine()) != null) {
oldtext += line + "\r\n";
}
reader.close();
String newtext = oldtext.replace("sanid", studentIds.get(i)).replace("salabel",studentTokens.get(i)); // Here the name "sanket" will be replaced by the current time stamp
FileWriter writer = new FileWriter("final.txt",true);
writer.write(newtext);
writer.close();
}
fstream.close();
br.close();
System.out.println("Done!!");
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error: " + e.getMessage());
}
}
The above code of mine reads data from a text file and query is a file that has a query in which 2 places "sanid" and "salabel" are replaced by the content of string array and writes another file final . But when i run the code the the final does not have the queries. but while debugging it shows that all the values are replaced properly.
but while debugging it shows that all the values are replaced properly
If the values are found to be replaced when you debugged the code, but they are missing in the file, I would suggest that you flush the output stream. You are closing the FileWriter without calling flush(). The close() method delegates its call to the underlying StreamEncoder which does not flush the stream either.
public void close() throws IOException {
se.close();
}
Try this
writer.flush();
writer.close();
That should do it.

Reading a text file in java

How would I read a .txt file in Java and put every line in an array when every lines contains integers, strings, and doubles? And every line has different amounts of words/numbers.
I'm a complete noob in Java so sorry if this question is a bit stupid.
Thanks
Try the Scanner class which no one knows about but can do almost anything with text.
To get a reader for a file, use
File file = new File ("...path...");
String encoding = "...."; // Encoding of your file
Reader reader = new BufferedReader (new InputStreamReader (
new FileInputStream (file), encoding));
... use reader ...
reader.close ();
You should really specify the encoding or else you will get strange results when you encounter umlauts, Unicode and the like.
Easiest option is to simply use the Apache Commons IO JAR and import the org.apache.commons.io.FileUtils class. There are many possibilities when using this class, but the most obvious would be as follows;
List<String> lines = FileUtils.readLines(new File("untitled.txt"));
It's that easy.
"Don't reinvent the wheel."
The best approach to read a file in Java is to open in, read line by line and process it and close the strea
// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console - do what you want to do
System.out.println (strLine);
}
//Close the input stream
fstream.close();
To learn more about how to read file in Java, check out the article.
Your question is not very clear, so I'll only answer for the "read" part :
List<String> lines = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("fileName"));
String line = br.readLine();
while (line != null)
{
lines.add(line);
line = br.readLine();
}
Common used:
String line = null;
File file = new File( "readme.txt" );
FileReader fr = null;
try
{
fr = new FileReader( file );
}
catch (FileNotFoundException e)
{
System.out.println( "File doesn't exists" );
e.printStackTrace();
}
BufferedReader br = new BufferedReader( fr );
try
{
while( (line = br.readLine()) != null )
{
System.out.println( line );
}
#user248921 first of all, you can store anything in string array , so you can make string array and store a line in array and use value in code whenever you want. you can use the below code to store heterogeneous(containing string, int, boolean,etc) lines in array.
public class user {
public static void main(String x[]) throws IOException{
BufferedReader b=new BufferedReader(new FileReader("<path to file>"));
String[] user=new String[500];
String line="";
while ((line = b.readLine()) != null) {
user[i]=line;
System.out.println(user[1]);
i++;
}
}
}
This is a nice way to work with Streams and Collectors.
List<String> myList;
try(BufferedReader reader = new BufferedReader(new FileReader("yourpath"))){
myList = reader.lines() // This will return a Stream<String>
.collect(Collectors.toList());
}catch(Exception e){
e.printStackTrace();
}
When working with Streams you have also multiple methods to filter, manipulate or reduce your input.
For Java 11 you could use the next short approach:
Path path = Path.of("file.txt");
try (var reader = Files.newBufferedReader(path)) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
Or:
var path = Path.of("file.txt");
List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);
Or:
Files.lines(Path.of("file.txt")).forEach(System.out::println);

Categories

Resources