i am working with android app to import the csv file to populate my DB.
I am using OpenCSV library to do it. I wish to use CSV reader to read from the inputstream.
I am using import feature from the gmail, so i use android:scheme=content
it makes csv file come as InputStream.
Inside I have 3 columns. Some rows have , character.
So when I try to separate columns using , I get error.
I wish help to know how to overcome this issue of comma inside the columns.
You could get your csv with another separator character? if yes, the you can call yor csv reader with this separator, from de docs...
CSVReader(Reader reader, char separator)
Constructs CSVReader with supplied separator.
If there is comma in columns, it gets difficult to read a csv file properly, I will show you the code that doesn't requires any library:
File f=new File("C:/Users/Public/"+filename+".csv");
Scanner sc=new Scanner(fc);
sc.useDelimiter("\r");
char cd='"';
//Create scanner string seperator ","
String g=String.valueOf(cd)+","+String.valueOf(cd);
//JOptionPane.showMessageDialog(null, "G: "+g);
String[] data=null;
while(sc.hasNextLine()){
data=null;
csvtemp=sc.nextLine().toString();
//remove first character ", string from second character
csvtemp=csvtemp.subString(1);
//remove last character ", string from first character to secondlast
csvtemp=csvtemp.subString(0,csvtemp.length()-1);
//Split record by seperator "," and copy to array
data=csvtemp.split(g);
}
sc.close();
Related
I've got a text file like this: Image
The first column represents the user ID and the last column represents balance. I want to iterate through the elements to find a specific user ID and then update their balance. I've managed to match the user ID and access the balance using the Scanner but I'm not sure how to update the balance in the text file with a new value. This is the code I've written so far:
File file = new File("UserInfo.txt");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
String info = sc.nextLine();
String data[] = info.split(" ");
//userId is a function argument
if(String.valueOf(userId).equals(data[0])){
//I want to update the balance in the text file with a new value here
}else{
continue;
}
}
sc.close();
it seems that you want to create a CSV file. My advice is to use ";" as a separator for the split() command, and if the file is not too big, read the whole file, put it into an arrayList of Strings and then manipulate It, rewriting the whole file at the end from the arrayList. Otherwise you have to use RandomAccess class
Replacing string in file is not so simple. You have to read whole file and rewrite part that you want to leave unchanged. Only when condition of comparing userID and data[0] is met, you would use replace() function to change user balance.
Your file is basically CSV file with space as line separator, as already mentioned store file contents in array or BufferedReader, change appropriate line and then export back to CSV.
I want to get specific lines of data from a text file using arraylist.
(more context: student details are stored in Student.txt.
if i have to update a specific student, i want to get that student's line from text file into an arraylist in order to edit it)
Text file looks like this (ID, Name, Degreelevel, Email, Conatctno : courses);
A30, sarah, sarah#gmail.com, +64732 ; Computer Science, Cyber Security, Digital Media
A45,zaha, zaha#gmail.com, +3683: Software Engineering
My code prints all the data in the text file into the arraylist.
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
ArrayList<String> lines = new ArrayList<>();
String line = reader.readLine();
while (line != null)
{
lines.add(line);
line = reader.readLine();
}
reader.close();
System.out.println(lines);
}
current output:[ID, Name, Degreelevel, Email, Conatctno : courses, A30, sarah, sarah#gmail.com, +64732 ; Computer Science, Cyber Security, Digital Media, A45,zaha, zaha#gmail.com, +3683: Software Engineering]
So how can i search a specific student ID and input only that line into arraylist. Any hint helps. Thank you.
You have to go over each line until you find your matching id. This is one of the reasons why we typically like to store this type of information in databases rather than text files.
Simply add an if-statement that checks you got the right line before you add it to the arraylist.
A very simple way of doing this would be:
String myId = "A30";
while (line != null){
if(line.startsWith(myId){
lines.add(line);
break;
}
line = reader.readLine();
}
Normally when parsing text files one would split up each line using some data delimiter, e.g. line.split(","), rather than using startsWith.
However, the data in your example is badly structured, using both colon, semi-colon and comma as delimiters. One common way of dealing with the problem that a string contains a delimiter is to encapsulate all string in quotation marks, and treat any special character found within quotation marks as a regular character.
A semi-formalized structured format for data in text file is csv. Most languages (including Java) has libraries for parsing csv files.
I have a csv file contain 78400 lines (25MB).
When I read the csv file line by line, 1 column has error in 2nd line.
It contains backslash character.
When I read this column, it read all the remaining columns in the csv file as single column.
"CDE","456","6346","testdata2","MyData2","ClassB"
"ABC","123","4567\","testdata","MyData","ClassA"
"CDE","456","6346","testdata2","MyData2","ClassB"
How to skip that line by using line seperator in java?
you can write method which would check by splitting the line into words and then identify the \ using as a char
String line=br.readline();
String words =line.split(",");
char[] word=words.toCharArray();
boolean escape=(word=='\');
You can identify the escape and handle it specially .
If you are using openCSV then just define your parser with an escape character other than backslash. If you don't want an escape character you can use the ICSVParser.NULL_CHARACTER or if you are using the 3.9 version of openCSV you can use the RFC4180Parser.
RFC4180ParserBuilder rfc4180ParserBuilder = new RFC4180ParserBuilder();
ICSVParser rfc4180Parser = rfc4180ParserBuilder.build();
CSVReaderBuilder builder = new CSVReaderBuilder(sr);
CSVReader reader = builder.withCSVParser(parser).build();
I am writing a Java program in which a tab separated values (TSV) file containing two columns of information is read by a BufferedReader and then split into two components (which will serve as [key,value] pairs in a HashMap later in the program) using String.split("\t"). Let's say the first line of the TSV file is as follows:
Key1\tHello world\nProgramming is cool\nGoodbye
The code shown below would separate this line into "Key1" and "Hello world\nProgramming is cool\nGoodbye":
File file = new File("sample.tsv");
BufferedReader br = new BufferedReader(new FileReader(file));
String s = br.readLine();
String[] tokens = new String[2];
tokens = s.split("\t");
The problem now comes in trying to print the second string (i.e. tokens[1]).
System.out.println(tokens[1]);
The line of code above results in the second string being printed with the newline characters (\n) being ignored. In other words, this is printed...
Hello world\nProgramming is cool\nGoodbye
...instead of this...
Hello worldProgramming is coolGoodbye
If I create a new string with the same text as above and use the String.equals() method to compare the two, it returns false.
String str = "Hello world\nProgramming is cool\nGoodbye";
boolean sameString = str.equals(tokens[1]); // false
Why can't special characters in the strings returned by String.split() be printed properly?
BufferedReader.readLine() read your string as one line, as that's how it's represented in the file. Buffered reader didn't read "\n" as ASCII(10) 0x0A, it read "ASCII(92) 0x9C ASCII(110) 0x6E".
If you type the input file the way you expect to see it with your text editor, it will print the way you expect.
on a unix like system:
echo -e "Hello world\nProgramming is cool\nGoodbye" > InputFile.result_you_want
echo "Hello world\nProgramming is cool\nGoodbye" > InputFile.result_you_get
You could use a program like echo to convert your TSV, but then you will need to split on the "\t" character, ASCII(9) 0x09, and not a literal "\t".
Split takes a regular expression. Escaping that tab character may be interesting.
"\t" or "\\t" may do the trick there.
If this is for work, you may want to use a tool or library to work around having to convert your file with echo.
String parsing in Java with delimeter tab "\t" using split has some suggestions there.
Searching for CSV java API's could be very useful. Most will let you set the delimiter character and information on line ending formats.
because in computer aspect, the text '\n' is not like the binary '\n'.
the first line of ur file, i think is like key1 Hello world\nProgramming\ncool
so it's the it can split the \t,but when it comes to print, it only show the text
'\n' but not the binary '\n' which will make the new Line
I'm new to reading text from a file.
I've got a task for which I need to print the amount of words which are in a file.
I'm using TextEdit on mac OS which ends in .rtf
When I run the following program, I get the output 5 even when the document is empty. When I add words, the count doesn't increment correctly.
Thanks.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Analyze{
public static void main(String[] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
int words = 0;
System.out.println("This is a word counter");
System.out.println("File name");
String filename = console.next();
File name = new File(filename);
Scanner int2 = new Scanner(name);
while (int2.hasNext()) {
String temp = int2.next();
words++;
}
System.out.println(words);
}
}
The problem is that you are reading a RTF file.
A 'blank' (as in no entered text) RTF file generated with TextEdit looks like this:
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf130
{\fonttbl}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
}
As you can see, the five lines correspond to the output of 5.
Either parse RTF in your program, which I doubt you want to do, or switch TextEdit to plaintext mode. See here
The file you're trying to count is an RTF file? Does it support italics, bold, font selection and things like that? In that case, it probably contains some data, even if there is no text. Your program does not care about the file format, so it naïvely reads everything as text.
Try running od or hexdump on your file (not sure if these exist on Mac OS X?) -- they print the exact bytes of a file. A truly empty file should not yield any output.
If your computer doesn't have the od or hexdump programs, you could try cat. It doesn't print the contents as numbers, so it doesn't give a 100% accurate view of special characters, but it should be able to demonstrate to you whether your file is empty or not.
Besides the RTF-Problem, also note that
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
with whitespace as in
A whitespace character: [ \t\n\x0B\f\r]
so the count is including tabs, newlines, etc. not only blanks