• 締切済み

java classの呼び出し方を教えて下さい。

java classの呼び出し方を教えて下さい。 public class test extends Applet implements Runnable,ActionListener,KeyListener{ Thread th; // スレッド宣言 AudioClip IdoSound,KaitenSound,KesiSound,SetSound,CrySound; public void init(){ }   ・   ・   ・ public void test(){ ここで、最下欄の class stream{ public static void main(String args[]){ } を実行させたいのですが、記入方法が解りません・・・ } public int kesiOne(int VP,int HP){ }    ・    ・    ・ class stream{ public static void main(String args[]){ try{ File file = new File("c:\\java\\data.txt"); if (checkBeforeWritefile(file)){ PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); pw.println("12345"); pw.close(); }else{ System.out.println("ファイルに書き込めません"); } }catch(IOException e){ System.out.println(e); } } private static boolean checkBeforeWritefile(File file){ if (file.exists()){ if (file.isFile() && file.canWrite()){ return true; } } return false; } } 解りにくい書き方で申し訳け有りませんが、宜しくお願いします。

  • Java
  • 回答数2
  • ありがとう数3

みんなの回答

  • salsberry
  • ベストアンサー率69% (495/711)
回答No.2

> ファイル出力されませんでした。 ANo.1にも書いたとおりAppletからローカルファイルを操作することは通常禁止されているので、そのせいでしょう。 ファイルアクセスを許可するにはセキュリティポリシーの変更が必要です。http://www.gadgety.net/shin/java/lesson5.htmlのSection 4を参考にしてください。

  • salsberry
  • ベストアンサー率69% (495/711)
回答No.1

argsの内容は全く使っていないようなので、 public void test() { stream.main(null); } でいいと思います。 ただしAppletでのローカルファイルの操作は通常禁止されているので、stream.main()を呼べてもSecurityExceptionが発生して思ったようには動かないかもしれません。

visitormap
質問者

お礼

早々にお教え頂き、有難う御座いました。 下記のように書き換えましたが、ファイル出力されませんでした。 勿論、コンパイルはOKでした。 間違いをご指摘頂ければ幸いです。 ( * が、書き換えた箇所です。) * public void test() { * stream.main(null); * } public void game(){ * test(); } class stream{ public static void main(String args[]){ try{ File file = new File("c:\\java\\test.txt"); int score=100; if (checkBeforeWritefile(file)){ PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); pw.println(12345); pw.close(); }else{ System.out.println("ファイルに書き込めません"); } }catch(IOException e){ System.out.println(e); } } private static boolean checkBeforeWritefile(File file){ if (file.exists()){ if (file.isFile() && file.canWrite()){ return true; } } return false; } }

関連するQ&A

  • public class Power {

    public class Power { static long pow(int a, int b){ if (b<=0) return 1; else return a*pow(a,b-1); } public static void main(String args[]){ System.out.println(pow(1,3)); } } run: 1 BUILD SUCCESSFUL (total time: 0 seconds) 簡単な問題ですが。。。 僕の予想では答えは2でした 1 * (1, 2) 1 * 1 * 2 じゃないんですか?

    • ベストアンサー
    • Java
  • Writer & PrintWriter

    import java.io.*; public class JavaIO4 { public static void main(String args[]) throws Exception { Writer w=new FileWriter(args[0]); PrintWriter pw=new PrintWriter(w); for (int i=0; i<=330; i+=30) { double atai=Math.sin(Math.PI*i/180.0); String str="sin("+i+")="+atai+"\n"; pw.write(str); } pw.close(); } } と import java.io.*; public class JavaIO4 { public static void main(String args[]) throws Exception { //Writer w=new FileWriter(args[0]); PrintWriter pw=new PrintWriter(args[0]); for (int i=0; i<=330; i+=30) { double atai=Math.sin(Math.PI*i/180.0); String str="sin("+i+")="+atai+"\n"; pw.write(str); } pw.close(); } } の結果が同じになるんですけど、 Writer w = new FileWriter(args[0]); PrintWriter pw = new PrintWriter(w); とするのはなぜなんですか? 何か意味はあるんですか?

  • Javaのカウント方法について

    お伺い致します。 CSVで取り込んだデータの抽出をしたいのですが、方法が見出せません。どの点を直せば宜しいのでしょうか。 具体的には取り込んだ郵便番号をカウントして(例:京都市,34)CSVファイルに出力するように出したいのですがカウントがうまくできません。(以下、コメントアウトしたものがありますが、今までのソースを記載します) 宜しく御願いします。 import java.io.FileReader; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.*; public class test01 { static String fname ="26KYOUTO.CSV"; public static void main(String[] args){ if(args.length>0) fname = args[0]; try { BufferedReader reader = new BufferedReader(new FileReader(fname)); BufferedWriter pw = new BufferedWriter(new PrintWriter("orig.txt")); String line = reader.readLine(); System.out.println(line); pw.println(""); int n = 0; int count = 0; _/* while(true) { String line = reader.readLine(); if(line.equals("26101")) break; count++; } */ reader.close(); System.out.println("京都府北区=" +count ); } catch(FileNotFoundException e) { System.out.println("ファイルがありません。"); } catch(IOException e) { System.out.println("入出力エラーです。"); } } }

    • ベストアンサー
    • Java
  • JAVAコンストラクタについて

    JAVA のコンストラクタ定義の際に、下記のようにコンストラクタにvoidを付けた時と付けない時の処理結果が変わるのはなぜでしょうか。 (1) class O { protected int d; O(){ System.out.println("O = " + d); } } class Study_6_3main_void { public static void main(String args[]) { int i = 1; O OO = new O(); System.out.println("O = "); } } (2) class O { protected int d; void O(){ System.out.println("O = " + d); } } class Study_6_3main_void { public static void main(String args[]) { int i = 1; O OO = new O(); System.out.println("O = "); } } 実行結果 (1) O = 0 O = (2) O = 以上です。回答の程よろしくおねがいします。

    • ベストアンサー
    • Java
  • java iを1づつ増やすプログラムと2づつ増やすプログラム

    次のようにすればiを1づつ増やして表示されます。 class Calc{   int i=1;   int add(){     return i++;   } } class Count{   public static void main(String[] args){     Calc calc = new Calc();     System.out.println("i = " + calc.add());     System.out.println("i = " + calc.add());     System.out.println("i = " + calc.add());   } } 実行結果 i = 1 i = 2 i = 3 しかし次のように2づつ増やそうとすると、 class Calc{   int i=1;   int add(){     return i+2;   } } class Count{   public static void main(String[] args){     Calc calc = new Calc();     System.out.println("i = " + calc.add());     System.out.println("i = " + calc.add());     System.out.println("i = " + calc.add());   } } 実行結果 i = 3 i = 3 i = 3 このようになってしまいます。どこがおかしいのでしょうか?

    • ベストアンサー
    • Java
  • Javaの質問です。

    データに0より小さい数値がある場合、その行を取り除いて集計させ、エラーがある行だけを別ファイルに書きだしたいのですが、うまくいきません。 なにかアドバイスいただけますでしょうか? よろしくお願いします。 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; class Data { double value; Data(double value) { this.value = value; } void setData(double value) { this.value = value; } public String toString() { return String.format("%5.1f", value); } } class Statistics { Data min = new Data(999.9); Data max = new Data(0.0); double total = 0.0; void add(double value) { if (min.value > value) { min.setData(value); } if (max.value < value) { max.setData(value); } total += value; } } class Category { String category; Statistics height = new Statistics(); Statistics weight = new Statistics(); Statistics bmi = new Statistics(); int num = 0; Category(String category) { this.category = category; } void add(double height, double weight) { this.height.add(height); this.weight.add(weight); this.bmi.add(weight / (height / 100.0 * height / 100.0)); num++; } public String toString() { return category + "," + "身長" + "," + "最高" + "," + height.max + "\n" + category + "," + "身長" + "," + "最低" + "," + height.min + "," + "\n" + category + "," + "身長" + "," + "平均" + "," + String.format("%5.1f\n", height.total / num) + category + "," + "体重" + "," + "最高" + "," + weight.max + "," + "\n" + category + "," + "体重" + "," + "最低" + "," + weight.min + "\n" + category + "," + "体重" + "," + "平均" + "," + String.format("%5.1f\n", weight.total / num) + category + "," + "BMI" + "," + "最高" + "," + bmi.max + "\n" + category + "," + "BMI" + "," + "最低" + "," + bmi.min + "\n" + category + "," + "BMI" + "," + "平均" + "," + String.format("%5.1f\n", bmi.total / num); } } class Health { Category male = new Category("男"); Category famale = new Category("女"); Category total = new Category("全体"); void add(String sex, double height, double weight) { if (sex.equals("男")) { male.add(height, weight); } else { famale.add(height, weight); } total.add(height, weight); } public String toString() { return "" + male + "\n" + famale + "\n" + total + "\n"; } } public class Main { static final String FILENAME = "hw.csv"; static final String SEPARATOR = ","; static final int SEX = 2, HEIGHT = 3, WEIGHT = 4; public static void main(String[] args) throws Exception { Health health = new Health(); String line; BufferedReader file = new BufferedReader(new FileReader(FILENAME)); for (int number = 1; (line = file.readLine()) != null; number++) { String[] item = line.split(SEPARATOR); try { if(Integer.parseInt(item[HEIGHT]) <= 0 || Integer.parseInt(item[WEIGHT]) <= 0 || item[HEIGHT] == null || item[WEIGHT] == null) { PrintWriter pw = new PrintWriter( new BufferedWriter (new FileWriter("out_error.csv"))); pw.println(number); pw.close(); health.add(item[SEX],Double.parseDouble(item[HEIGHT]),Double.parseDouble(item[WEIGHT])); } } catch (Exception e) { } } file.close(); PrintWriter pw = new PrintWriter( new BufferedWriter (new FileWriter("out.csv"))); pw.println(health); pw.close(); } }

  • Java・ファイルへの書き込み

    はじめまして。 下記のプログラムで、以下の3つがどのような関係なのかがわかりません。 ("test1.txt")とnew FileWriterの関係 (new FileWriter("test1.txt")とnew BufferedWriterの関係 (new BufferedWriter(new FileWriter("test1.txt")と new PrintWriterの関係 どうかご教示いただきたく、よろしくお願い致します。 import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Sample7 { public static void main(String[] args){ PrintWriter pw = null; try{ pw = new PrintWriter (new BufferedWriter(new FileWriter("test1.txt"))); pw.println("Hello!"); pw.println("GoodBye"); System.out.println("ファイルに書き込みました。"); }catch(IOException e){ System.out.println("入出力エラーです。"); }finally{ if(pw != null){ pw.close(); } } } }

    • ベストアンサー
    • Java
  • javaのカウントアップについて

    javaのEclipseを使っています。 カウントアップ後、表示する度に、 1, 2, 3, …と表示するプログラムを作りたいです。 プログラム例を教えて下さい。 public class Test { public static void main(String[] args) { Count count = new Count(); int scount = count.read(); count.write(scount); } } class Count { int count = 1; int read() { return count++; } void write(int scount) { System.out.println(scount); } }

  • javaプログラミングの質問です。

    プログラムで数値の奇遇、合計値、最大値までは出せたのですが平均値の出し方がわかりません。 どこに何を入れればいいかを教えてください。お願いします。 public class pazu{ public static void main(String[] args){ int sum =0,saidai; System.out.println("コマンドラインパラメータは"+args.length+"個です"); for(int i=0;i<args.length;i++){ int x=Integer.parseInt(args[i]); if(pazu.is_even(x)) System.out.println(args[i]+"は偶数です"); else System.out.println(args[i]+"は奇数です"); sum+=x; } saidai=pazu.max(args); System.out.println("合計:"+sum); System.out.println("最大:"+saidai); } static boolean is_even(int number){ return number%2==0; } static int max(String[] number){ int max =0; for(int i=0;i<number.length;i++){ if(max<Integer.parseInt(number[i])){ max=Integer.parseInt(number[i]); } } return max; } }

    • ベストアンサー
    • Java
  • Javaのプログラミングで困っています。

    プロフィールを打ち込んで表示するプログラムを書いていました。 プロフィールの打ち込み後に画面に表示するのと同時にテキストファイルに書き出したいと思ったため、書いてみましたが、ファイルに書き込めず知識不足で困っています。 是非教えていただきたく書き込みをさせていただきました。 自分で書いたコードは書きになります。 import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; class Sample extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; static JTextField name; static JTextField birth; static JTextField address; static JTextField mail; static JTextField number; JLabel label; public static void main(String args[]){ Sample frame = new Sample("profile"); frame.setVisible(true); } Sample(String title){ setTitle(title); setBounds(100, 100, 500, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); name = new JTextField("名前", 20); birth = new JTextField("生年月日", 20); address = new JTextField("住所", 40); mail = new JTextField("mail", 40); number= new JTextField("電話番号", 30); JButton button = new JButton("取得"); button.addActionListener(this); label = new JLabel(); p.add(name); p.add(birth); p.add(address); p.add(mail); p.add(number); p.add(button); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); contentPane.add(label, BorderLayout.SOUTH); try{ File file = new File("C:Users\\mattun\\Documents\\sample.txt"); if (checkBeforeWritefile(file)){ PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); //ファイルに書き込む内容 pw.println("名前:"+name.getText()); pw.println("生年月日:"+birth.getText()); pw.println("住所:"+address.getText()); pw.println("mail:"+mail.getText()); pw.println("電話:"+number.getText()); pw.close();//ファイルを閉じる }else{ System.out.println("ファイルに書き込めません");//書き込み出来ない場合 } }catch(IOException e){ System.out.println(e); } } public void actionPerformed(ActionEvent e){ label.setText("<html>"+"名前:"+name.getText()+"<br>"+"生年月日:"+birth.getText()+"<br>"+"住所:"+address.getText()+ "<br>"+"mail:"+mail.getText()+"<br>"+"電話:"+number.getText()+"</html>"); } private static boolean checkBeforeWritefile(File file){ if (file.exists()){ if (file.isFile() && file.canWrite()){ return true; } } return false; } }

    • ベストアンサー
    • Java