Read a text file with Arab characters
in
Programming Questions
•
2 months ago
How to read a text file with Arab characters encoded in UTF-8 with processing?
I use InputStreamReader and I work under Windows.
code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
String[] lines = new String[0];
void setup() {
size(300,300);
}
void draw() {
}
void keyPressed() {
lines = new String[0];
String filename = "arabe1.txt";
File f = new File(dataPath(filename));
if(f.exists()) {
//lines = loadStrings(filename);
try {
/* Open a stream to a File (in your data Folder) here */
InputStream fi = createInput(filename);
/* get a reader with your encoding */
InputStreamReader input = new InputStreamReader( fi, Charset.forName("UTF-8") );
BufferedReader reader = new BufferedReader(input);
// read the file line by line
String line;
int counter = 0;
while ((line = reader.readLine()) != null) {
lines = append(lines, line);
counter++;
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
for ( int i = 0 ; i < lines.length;i++) println(lines[i]);
}
I use InputStreamReader and I work under Windows.
code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
String[] lines = new String[0];
void setup() {
size(300,300);
}
void draw() {
}
void keyPressed() {
lines = new String[0];
String filename = "arabe1.txt";
File f = new File(dataPath(filename));
if(f.exists()) {
//lines = loadStrings(filename);
try {
/* Open a stream to a File (in your data Folder) here */
InputStream fi = createInput(filename);
/* get a reader with your encoding */
InputStreamReader input = new InputStreamReader( fi, Charset.forName("UTF-8") );
BufferedReader reader = new BufferedReader(input);
// read the file line by line
String line;
int counter = 0;
while ((line = reader.readLine()) != null) {
lines = append(lines, line);
counter++;
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
for ( int i = 0 ; i < lines.length;i++) println(lines[i]);
}
2