プロが教える店舗&オフィスのセキュリティ対策術

Java初心者です。

今、下記のようなプログラムを作成しました。

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;

class RTFView
extends JFrame
{
public RTFView()
{
setTitle( "RTF Text Application" );
setSize(1200, 720);
setBackground( Color.gray );
getContentPane().setLayout( new BorderLayout() );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel, BorderLayout.CENTER );

// Create an RTF editor window
RTFEditorKit rtf = new RTFEditorKit();
JEditorPane editor = new JEditorPane();
editor.setEditorKit( rtf );
editor.setBackground( Color.white );

// This text could be big so add a scroll pane
JScrollPane scroller = new JScrollPane();
scroller.getViewport().add( editor );
topPanel.add( scroller, BorderLayout.CENTER );

// Load an RTF file into the editor
try
{
FileInputStream fi = new FileInputStream( "test.rtf" );
rtf.read( fi, editor.getDocument(), 0 );
}
catch( FileNotFoundException e )
{
//ファイルがない場合 cmd 上に(" ")内が表示される
System.out.println( "File not found" );
JEditorPane ep1 = new JEditorPane();
}
catch( IOException e )
{
System.out.println( "I/O error" );
}
catch( BadLocationException e )
{
}
}

public static void main( String args[] )
{
// Create an instance of the test application
RTFView mainFrame= new RTFView();
mainFrame.setVisible( true );
}
}

コンパイル後、test.rtfを読み込むものにしましたが、
読込後、日本語が文字化けしてしまっています。
どのように対処すればよいのでしょうか?

検索してもなかなか見つからないので質問させていただきました。
ご回答の程よろしくお願いいたします。

A 回答 (1件)

RTFEditorKitはユニコードしか読めないが、読み込むファイルの文字コードはどうなっているだろうか?

    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!