This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Read/write .txt file with special characters
Im reading a file but I dont know how to read the accents and special caracters, here is mo code for read I read I have to add a different codification but i dont know how to doit
File file = new File("C://fichero.csv");
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
line = bufRdr.readLine();
Thanks
Try with the following:
File file = new File("C://fichero.csv");
BufferedReader bufRdr = new BufferedReader(
new InputStreamReader(new FileInputStream(file),"ISO-8859-1"));
String line = null;
line = bufRdr.readLine();
Related
This question already has answers here:
How to read a specific line using the specific line number from a file in Java?
(19 answers)
Closed 5 years ago.
My program is reading all lines in the file but i just need the second one.
String line;
try (
InputStream fis = new FileInputStream(source);
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr)) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
If you only need the second line and you are sure the file always has at least two lines, you can just read twice and ignore the first time.
br.readLine(); //read, but ignore
System.out.println(br.readLine()); // read and output
This question already has answers here:
Reading a plain text file in Java
(31 answers)
Closed 5 years ago.
I have text file which i have read once user upload, how can i read using java code, can any one give some suggestion which would very helpful to me
sample file looks like below
SNR Name
1 AAR
2 BAT
3 VWE
A common pattern is to use
try (BufferedReader br = new BufferedReader(new FileReader(file)) ) {
String line;
while ((line = br.readLine()) != null) {
// process the line to insert in database.
}}
The easiest way is use Scanner() object
Scanner sc = new Scanner(new File("myFile.txt"));
use
boolean hasNext = sc.hasNext();
to know if there are more items in the file
and
String item = sc.next();
to get items secuentially.
I attach the documentation (it provides very good code examples)
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
You can use BufferedReader to read file line by line.
So, even if your file is too big, then also it will read line by line only. It won't load entire file. Declaration will be similar to this.
BufferedReader br = new BufferedReader(new FileReader(FILENAME));
And
you can use command
while ((sCurrentLine = br.readLine()) != null) { .....// process
}
to read file till end.
Obviously, you have to use seperator in file to split the records in each line. And store accordingly in java objects.
After that you can store in DB through DAO.
This question already has answers here:
How to append text to an existing file in Java?
(31 answers)
Closed 6 years ago.
How can I write a new row of data to a .CSV file that already has data in it. So far my code just clears the file and doesn't actually write anything?
BufferedReader br = null;
BufferedWriter bw = null;
String fileString = "patients.csv";
String fileLine = "";
File file = new File(fileString);
br = new BufferedReader(new FileReader(file));
FileWriter fw = new FileWriter(fileString);
bw = new BufferedWriter(fw);
while((fileLine = br.readLine()) != null){
bw.write(fileLine);
}
br.close();
bw.close();
specify true in the constructor java FileWriter to know that the true and append be added to the end of the file if you place it does not overwrite information
FileWriter fw = new FileWriter(fileString,true);
If you want to append to a file using FileWriter then use this Constructor
As per Javadocs
Constructs a FileWriter object given a File object. If the second
argument is true, then bytes will be written to the end of the file
rather than the beginning.
But, back to your code, you will only need to write new data to the FileWriter and not rewrite existing data.
This question already has answers here:
android reading from a text file
(3 answers)
How to load text from file to textview [duplicate]
(3 answers)
Closed 9 years ago.
My problem with the software I'm trying to make is this;
when I save the information, it's saved with this code:
With this, the information is saved in a text in the SD,
but I couldn't to read that information. I've tried with several ways.
I want to save the content of the TXT in a String to put it in a TextView.
try
{
String myData ;
FileInputStream fis = new FileInputStream(your file name);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// play with myData..It's file content
I have tried this code and it works perfectly :D
File ruta_sd = Environment.getExternalStorageDirectory();
File f = new File(ruta_sd.getAbsolutePath(), "datos.txt");
BufferedReader fin =new BufferedReader(
new InputStreamReader(
new FileInputStream(f)));
String texto = fin.readLine();
fin.close();
textohere.setText(texto);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a Java String from the contents of a file
I have a .txt file that I want to save in a String variable. I imported the file with File f = new File("test.txt");. Now I am trying to put the contents of it in a String variable. I can not find a clear explanation for how to do this.
Use a Scanner:
Scanner file = new Scanner(new File("test.txt"));
String contents = file.nextLine();
file.close();
Of course, if your file has multiple lines you can call nextLine multiple times.
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}