Answer the question
In order to leave comments, you need to log in
Problem with encoding in console and file?
I read the data from the file, the Russian characters turned into squares when I printed them to the console, I thought that I would write to the file ok, but there I also
read the squares like this in a utf-8 file like this:
Z:\Автоматизация\музыка для радио \Solarsoul\[2013] Defying Gravity [AR0003] WEB\11. Solarsoul - UDFy (13 Billion Light Years From Earth).mp3
633.6062585034014
Z:\Автоматизация\музыка для радио \Solarsoul\[2013] Defying Gravity [AR0003] WEB\02. Solarsoul - About Eternal Wandering and About The Earth (Memory of Ray Bradbury).mp3
613.564081632653
Z:\Автоматизация\музыка для радио \Solarsoul\[2013] Defying Gravity [AR0003] WEB\01. Solarsoul - A Star Is Born (Original Mix).mp3
328.80625850340135
Z:\Автоматизация\музыка для радио \Solarsoul\[2013] Defying Gravity [AR0003] WEB\14. Solarsoul - Defying Gravity (Nale Remix).mp3
304.99410430839004
...
try {
File file = new File("C:\\Users\\User\\source\\repos\\Project1\\Project1\\playlist4.m3u8");
//создаем объект FileReader для объекта File
FileReader fr = new FileReader(file);
//создаем BufferedReader с существующего FileReader для построчного считывания
BufferedReader reader = new BufferedReader(fr);
// считаем сначала первую строку
Type tmp = new Type();
tmp.length = (float) 0.0;
tmp.name = "";
String line = reader.readLine();
while (line != null) {
tmp.name = line;
line = reader.readLine();
tmp.length = Float.parseFloat(line);
music.add(tmp);
//System.out.println(tmp.name+"\n"+tmp.length);
// считываем остальные строки в цикле
line = reader.readLine();
tmp = new Type();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try(FileWriter writer = new FileWriter("playlist.m3u8", false))
{
writer.write("#EXTM3U\n");
for (int i = 0; i < vec.size(); i++)
{
writer.write("#EXTINF:");
writer.write((int)vec.get(i).length);
writer.write(",");
writer.write(vec.get(i).name);
writer.write("\n");
writer.write(vec.get(i).name);
writer.write("\n");
}
writer.flush();
}
catch(IOException ex){
System.out.println(ex.getMessage());
}
Answer the question
In order to leave comments, you need to log in
The file encoded "windows-1251" was a little mistaken
try {
File file = new File("C:\\Users\\User\\Desktop\\Tropic-Island\\Tropic-Island\\C++\\Project1\\Project1\\playlist4.m3u8");
//создаем объект FileReader для объекта File
//FileReader fr = new FileReader(file);
FileInputStream fStream = new FileInputStream(file);
InputStreamReader fr = new InputStreamReader(fStream, "windows-1251");
//создаем BufferedReader с существующего FileReader для построчного считывания
BufferedReader reader = new BufferedReader(fr);
// считаем сначала первую строку
Type tmp = new Type();
tmp.length = (float) 0.0;
tmp.name = "";
String line = reader.readLine();
while (line != null) {
tmp.name = line;
line = reader.readLine();
tmp.length = Float.parseFloat(line);
music.add(tmp);
//System.out.println(tmp.name+"\n"+tmp.length);
// считываем остальные строки в цикле
line = reader.readLine();
tmp = new Type();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("playlist.m3u8"), "windows-1251")))
{
writer.write("#EXTM3U\n");
for (int i = 0; i < vec.size(); i++)
{
writer.write("#EXTINF:");
writer.write(Integer.toString((int)vec.get(i).length));
writer.write(",");
writer.write(vec.get(i).name);
writer.write("\n");
writer.write(vec.get(i).name);
writer.write("\n");
}
writer.flush();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question