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.
Related
I have Created One project.Buta i get data in console i want set data to textarea
File[] F=File.listFiles();
for (File File1:F) {
FileInputStream fstream = null;
String strLine ;
try {
fstream = new FileInputStream(File1);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
while ((strLine = br.readLine()) != null)
System.out.println (strLine);
String str=strLine;
final TextArea txt=new TextArea(str);
layout.addComponents(txt);
//br.close();
} catch (IOException e) {
e.printStackTrace();
}
You must accumulate the read lines, so you can then later on add it to the text area.
You might consider what to do with line breaks, currently they just get sorted out of the final string/text.
for (File File1:F)
{
FileInputStream fstream = null;
String strLine;
StringBuilder sb= new StringBuilder();
try
{
fstream = new FileInputStream(File1);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
while ((strLine = br.readLine()) != null)
{
System.out.println (strLine);
sb.append(str);
}
final TextArea txt=new TextArea(sb.toString());
layout.addComponents(txt);
//br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
I want to fill an array with names. The array should be filled depending on what gender the character is.
public void fillNameArray() throws IOException {
Character character = new Character(); //Declare and initialise character object
if(character.getGender() == "F"){
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("femaleNames.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
String[] array = (String[]) lines.toArray();
}
else{
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("maleNames.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
String[] array = (String[]) lines.toArray();
}
}
The problem is in the following line:
String[] array = (String[]) lines.toArray();
Write instead:
String[] array = lines.toArray(new String[lines.size()]);
See post as example.
Please can you also move the read code on a own method? So less code duplication.
public List<String> readNames(String file) {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
return lines;
}
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();
I read a file by using bufferreader and also have another array(array_to_compare_with). So, I want to compare these two data.
How can I get over this ? Anybody has a clue ?
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
// strLine and array_to_compare_with comparison needed to be done
}
Maybe you want something like this:
String[] array_to_compare_with = { "str1","str2","str3","str4" };
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
boolean found = false;
for(String str : array_to_compare_with){
if(str.equalsIgnoreCase(strLine.trim())){
found = true;
break;
}
}
if(found){
//DO WHAT YOU WHANT
}
}
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
for(String s: array_to_compare_with){
if(strLine.contains(s)){
System.out.println(strLine);
break;
}
}
}
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);
}
}