忍者ブログ
凡人の日々の経過を記録
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



文字コードを変換するメソッドを作ってみました。
引数に入力ファイル名、出力ファイル名、変換する文字コード、改行コード(タイプ)を指定できるようにしたメソッドを用意してみました。

-------------------------------------------------------
import java.io.*;

public class ChMojiCode {
    public static void main(String[] args) {
        try {
            if (args.length > 1) {
                //changeMojiCode(args[0], args[1], "ISO2022JP", 0);
                //changeMojiCode(args[0], args[1], "Shift_JIS", 0);
                //changeMojiCode(args[0], args[1], "EUC_JP", 2);
                changeMojiCode(args[0], args[1], "UTF-8", 2);
                //changeMojiCode(args[0], args[1], "UTF-16", 2);
            }
            else {
                System.err.println( "Usage : java ChMojiCode <InFile> <OutFile>" );
            }
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    public static void changeMojiCode(
            String inStr, String outStr, String outCode, int endType)
        throws IOException {

        FileInputStream inFile = new FileInputStream(inStr);
        FileOutputStream outFile = new FileOutputStream(outStr);
        InputStreamReader inStm = new InputStreamReader(inFile, "JISAutoDetect");
        OutputStreamWriter outStm = new OutputStreamWriter(outFile, outCode);
        BufferedReader inBuf = new BufferedReader(inStm);
        BufferedWriter outBuf = new BufferedWriter(outStm);

        String lineStr;
        String endStr;
        if (endType == 0)      { endStr = "\r\n"; }
        else if (endType == 1) { endStr = "\r"; }
        else if (endType == 2) { endStr = "\n"; }
        else                   { endStr = "\n"; }
        while ((lineStr = inBuf.readLine()) != null) {
            outBuf.write( lineStr + endStr );
        }

        outBuf.flush();
        inBuf.close();
        outBuf.close();
    }
}
-------------------------------------------------------

拍手[0回]

PR


イベント処理の簡単なプログラムを作ってみました。


------------------------------------------
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Simple00 extends JFrame implements ActionListener {

    private JLabel label;
    private JButton button;

    public Simple00() {
        setTitle("シンプルフレーム");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(320,200);
        setLocationRelativeTo(null);

        label = new JLabel("ラベル");
        getContentPane().add(label, BorderLayout.CENTER);

        button = new JButton("ボタン");
        getContentPane().add(button, BorderLayout.SOUTH);

        button.addActionListener(this);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == button) {
            label.setText("Pushed!!!");
        }
    }

    public static void main(String[] args) {
        new Simple00();
    }
}
------------------------------------------

Simple00Frame01.png

拍手[0回]



Swingの始めということでフレームを作成してみました。

  • Simple00.java
-----------------------------------------------------------------------------------------------
import javax.swing.JFrame;

public class Simple00 extends JFrame {

  public Simple00() {
    setTitle("シンプルフレーム");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(320,200);
    setLocationRelativeTo(null);

    setVisible(true);
  }

  public static void main(String[] args) {
    new Simple00();
  }


}
-----------------------------------------------------------------------------------------------

フレームのみを表示するシンプルなプログラムです。



Simple00Frame.png





拍手[0回]




Powered by 忍者ブログ  Design by © まめの
Copyright © [ ずくのない凡人の日記 ] All Rights Reserved.
http://bambooflow.blog.shinobi.jp/