LINUX.ORG.RU

Вопрос по Java (KOI8 и DataInputStream)


0

0

Привет!

Имею текстов?й файл в KOI8-R. Задача - перекинуть файл клиенту и записать там на диск. Написал все на C - прекрасно, но сказали переделать под java.

В сервере:

DataInputStream s = new DataInputStream(new BufferedInputStream(new FileInputStream("out.file")));

while((str = s.readLine()) != null) { //читаю в StringBuffer }

// и отс?лаю.

В клиенте:

DataInputStream is = new DataInputStream(new BufferedInputStream(cSocket.getInputStream()));

while((str = is.readLine()) != null)

{

sBuffer.append(str);

sBuffer.append("\n"); //System.out.println("ready for input"); }

Хотя readLine тут deprecated - удобно. (Нахрена его депрекатить?). Вобщем, все работает только одна проблема: KOI8-символ превращается в херню. Как с ?тим бороться?

Спасибо.

anonymous

http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html

Цитата:

Remember that FileReader and FileWriter read and write 16-bit characters. However, most native file systems are based on 8-bit bytes. These streams encode the characters as they operate according to the default character-encoding scheme. You can find out the default character-encoding by using System.getProperty("file.encoding"). To specify an encoding other than the default, you should construct an OutputStreamWriter on a FileOutputStream and specify the encoding. For information about encoding characters, see the Internationalization trail.

anonymous
()

>Нахрена его депрекатить?

Если звезды зажигают, значит это кому-то нужно.

anonymous
()

>>Хотя readLine тут deprecated - удобно

String readLine() 

Deprecated. This method 
 ___  does not properly convert bytes to characters. ____ 
As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form: 

     DataInputStream d = new DataInputStream(in);
with: 
     BufferedReader d
          = new BufferedReader(new InputStreamReader(in))

Зачем вообще читать данные построчно в стринг буффер ?

DataInputStream in = new DataInputStream(new FileInputStream("input.txt"));
byte[] data = new byte[1024];
int len = 0;
while ((len = in.read(data)) != -1){
     пересылаем массив байтов клиенту
}

jk_
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.