OpenCSV CSVReader UTF-8 encoding - java

I'm trying to read a CSV file with the following characters: â/ô/etc. My code isn't parsing these characters well. I'm getting the � character/symbol instead of the real character.
This is the code I'm using for reading the CSV file:
private List<String[]> getRows(File f) throws IOException {
//FileReader fileReader = new FileReader(f);
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(f), "UTF-8");
try {
CSVReader reader = new CSVReader(inputStreamReader, ';');
try {
return reader.readAll();
} finally {
reader.close();
}
} finally {
inputStreamReader.close();
}
}
Who can help me? Thanks!

Try something like below.
File file = new File("H:\\file name.csv");
BufferedReader br = new BufferedReader(new FileReader(file));
int lineNumber = 0;
while ((line = br.readLine()) != null) {
lineNumber++;
if ( line.trim().length() == 0 ) {
continue;
}
arr=line.split(",");
for (int j=0;j<arr.length;j++)
{
ft=arr[j];
ft=ft.trim();
System.out.print(ft);
}
}

Related

extract a part of txt file from String name to an empty line

I want to extract a part of a text file beginning with a String name and ending with an empty line
Here is what I tried:
public File CreateSubscripberFile(File file,String name) throws IOException
{
FileWriter fw = new FileWriter(f);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
if(line.equals(name))
{
while (((line = br.readLine()) != null) && (!(line.equals("\n"))))
{
fw.write(line);
System.out.println(line);
}
}
}
br.close();
fr.close();
fw.close();
return f;
}
but I get the original file as result!
Try to replace:
(line = br.readLine())!=null)
with:
(!(line = br.readLine()).equals(""))
null in this example would mean 'there is not more lines', and .equals(""), looks for empty line.
it's done by edit he code like this
public File CreateSubscripberFile(File file,String name,String fname) throws IOException
{
File f= new File(fname);
FileWriter fw = new FileWriter(f);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null){
if(line.equals(name))
{
String line1 = br.readLine() ;
while(line1 != null && !line1.isEmpty() )
{
System.out.println(line1);
line1=br.readLine();
}
}
}
br.close();
fw.close();
return f;
}

How can I ignore a blank line in a srt file using Java

I have a srt file like below and I want to remove blank line : in line no 3
**
1
Line1: 00:00:55,888 --> 00:00:57,875.
Line2:Antarctica
Line3:
Line4:2
Line5:00:00:58,375 --> 00:01:01,512
Line6:An inhospitable wasteland.
**
FileInputStream fin = new FileInputStream("line.srt");
FileOutputStream fout = new FileOutputStream("m/line.srt");
int i = 0;
while(((i =fin.read()) != -1)){
if(i != 0)
fout.write((byte)i);
}
There you go. Steps:
1) FileInputStream fin = new FileInputStream("line.srt"); this is to get the file to a bufferedreader in the next step
2) BufferedReader reader = new BufferedReader(new InputStreamReader(fin)); get the text file to a buffereader
3) PrintWriter out = new PrintWriter("newline.srt"); use a print writer to write the string of every line in the new text file
4) String line = reader.readLine(); read next line
5) while(line != null){
if (!line.trim().equals("")) { check that line is not null and that line is not empty
6) out.println(line); write line (not empty) to the output .srt file
7) line = reader.readLine(); get new line
8) out.close(); close PrintWriter in the end...
import java.io.*;
class RemoveBlankLine {
public static void main(String[] args) throws FileNotFoundException, IOException{
FileInputStream fin = new FileInputStream("line.srt");
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
PrintWriter out = new PrintWriter("newline.srt");
int i = 0;
String line = reader.readLine();
while(line != null){
if (!line.trim().equals("")) {
out.println(line);
}
line = reader.readLine();
}
out.close();
}
}
INPUT:
**
1
00:00:55,888 --> 00:00:57,875.
Antarctica
2
00:00:58,375 --> 00:01:01,512
An inhospitable wasteland.
**
OUTPUT:
**
1
00:00:55,888 --> 00:00:57,875.
Antarctica
2
00:00:58,375 --> 00:01:01,512
An inhospitable wasteland.
**
By the way, make sure you are clear when you ask your questions, because the way you state your problem I assumed Line1, Line2, etc are part of your input file, and I have prepared another solution which I had to change... Make sure you are clear and precise so that you get the proper answers !
You can try something like :
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("line.srt"));
bw = new BufferedWriter(new FileWriter("m/line.srt"));
for(String line; (line = br.readLine()) != null; ) {
if(line.trim().length() == 0) {
continue;
} else {
bw.write(line);
bw.newLine();
}
}
bw.flush();
bw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
hope this help
public static void main(String[] args) throws FileNotFoundException, IOException {
Path myPath = Paths.get("e:\\", "1.txt");
List<String> ls ;
ls = Files.readAllLines(myPath, StandardCharsets.US_ASCII);
PrintWriter out = new PrintWriter("e:\\2.txt");
for (int i = 0; i < ls.size(); i++) {
String []temp = ls.get(i).split(":");
if(temp.length>1) {
out.println(ls.get(i));
}
}
out.close();
}

BufferedReader to skip first line

I am using the following bufferedreader to read the lines of a file,
BufferedReader reader = new BufferedReader(new FileReader(somepath));
while ((line1 = reader.readLine()) != null)
{
//some code
}
Now, I want to skip reading the first line of the file and I don't want to use a counter line int lineno to keep a count of the lines.
How to do this?
You can try this
BufferedReader reader = new BufferedReader(new FileReader(somepath));
reader.readLine(); // this will read the first line
String line1=null;
while ((line1 = reader.readLine()) != null){ //loop will run from 2nd line
//some code
}
You can use the Stream skip() function, like this:
BufferedReader reader = new BufferedReader(new FileReader(somepath));
Stream<String> lines = reader.lines().skip(1);
lines.forEachOrdered(line -> {
...
});
File file = new File("path to file");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int count = 0;
while((line = br.readLine()) != null) { // read through file line by line
if(count != 0) { // count == 0 means the first line
System.out.println("That's not the first line");
}
count++; // count increments as you read lines
}
br.close(); // do not forget to close the resources
Use a linenumberreader instead.
LineNumberReader reader = new LineNumberReader(new InputStreamReader(file.getInputStream()));
String line1;
while ((line1 = reader.readLine()) != null)
{
if(reader.getLineNumber()==1){
continue;
}
System.out.println(line1);
}
You can create a counter that contains the value of the starting line:
private final static START_LINE = 1;
BufferedReader reader = new BufferedReader(new FileReader(somepath));
int counter=START_LINE;
while ((line1 = reader.readLine()) != null) {
if(counter>START_LINE){
//your code here
}
counter++;
}
You can do it like this:
BufferedReader buf = new BufferedReader(new FileReader(fileName));
String line = null;
String[] wordsArray;
boolean skipFirstLine = true;
while(true){
line = buf.readLine();
if ( skipFirstLine){ // skip data header
skipFirstLine = false; continue;
}
if(line == null){
break;
}else{
wordsArray = line.split("\t");
}
buf.close();

UTF-8 : characters not recognized

I am trying to parse a text file using HashMap. The problem is that I have special characters like Ș and Ț and my application didn't recognize them.
This is my code:
Map<String, String> m = new LinkedHashMap<String, String>();
FileInputStream fin = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fin = new FileInputStream("inferredflexforms.txt");
isr = new InputStreamReader(fin, "UTF-8");
br = new BufferedReader(isr);
String line = br.readLine();
while (line != null) {
String[] toks = line.split("\\s+");
m.put(toks[0], toks[1]);
line = br.readLine();
}
} finally {
if (br != null) { br.close(); }
if (isr != null) { isr.close(); }
if (fin != null) { fin.close(); }
}
System.out.println(m);
My text file contains: dănțaseși dansa
And my output is: dăn?ase?i=dansa
The "ș" and "ț" was replaced by "?".
What should I do?
Thank you.

Reading txt file contents and storing in array

I have been trying to read a txt file. The txt file contains lines e.g
First Line
Second Line
Third Line
.
.
.
Now I am using following code
InputStream is = null;
try {
is = getResources().getAssets().open("myFile.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<String> arrayOfLines = new ArrayList<String>();
Reader reader;
//char[] buffer = new char[2048];
try {
Reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read()) != -1) {
}
}catch (Exception e) {
e.printStackTrace();
}
My question is, how can i store each line in the arrayList. Ofc We have to use a check for "/n" but how.
You could alternatively use the Scanner class.
Scanner in = new Scanner(new File("/path/to/file.txt"));
while(in.hasNextLine()) {
arrayOfLines.add(in.nextLine());
}
You don't have to worry about \n, since Scanner.nextLine() will skip the newline.
This code should work.
ArrayList<String> arrayOfLines = new ArrayList<String>();
FileInputStream fstream = new FileInputStream("myfile.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
arrayOfLines.add(strLine);
}
This:
int n;
while ((n = reader.read()) != -1) {
}
Should probably look more like this:
String line = reader.readLine();
while (line!=null) {
arrayOfLines.add(line);
line = reader.readLine();
}
Since you are using a BufferedReader, you should be calling readLine() instead of reading into a char buffer. The Reader declaration also needs to be BufferedReader.

Categories

Resources