java 代码问题

学生管理系统
https://blog.csdn.net/howard2005/article/details/79336373
计算器 :https://blog.csdn.net/without_scruple/article/details/78603605
https://blog.csdn.net/CCSUXWZ/article/details/78239089

枚举 : https://blog.csdn.net/qq_27093465/article/details/52180865

public class lei {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       String s1 = "abc";
       String s2 = new String ("abc");
       System.out.println(s1);
       System.out.println(s2);//s1有一个对象  s2有两个对象
       System.out.println(s1==s2);//false
       System.out.println(s1.equals(s2));//true
    }
}

2.排序
https://www.cnblogs.com/onepixel/articles/7674659.html
3.创建一个文件

 String dirname = "C:\\a";
        File d = new File(dirname);
        // 现在创建目录
        d.mkdirs();

4.文档的输入输出

import java.io.*;
 
public class iodemo {
    public static void main(String args[]) {
        try {
            byte bWrite[] = { 11, 21, 3, 40, 5 };
            OutputStream os = new FileOutputStream("test.txt");
            for (int x = 0; x < bWrite.length; x++) {
                os.write(bWrite[x]); // writes the bytes
            }
            os.close();
 
            InputStream is = new FileInputStream("test.txt");
            int size = is.available();
 
            for (int i = 0; i < size; i++) {
                System.out.print((char) is.read() + "  ");
            }
            is.close();
        } catch (IOException e) {
            System.out.print("Exception");
        }
    }
}

5.读取目录
一个目录其实就是一个 File 对象,它包含其他文件和文件夹。
如果创建一个 File 对象并且它是一个目录,那么调用 isDirectory() 方法会返回 true。
可以通过调用该对象上的 list() 方法,来提取它包含的文件和文件夹的列表。
下面展示的例子说明如何使用 list() 方法来检查一个文件夹中包含的内容:

import java.io.File;
 
public class iodemo{
    public static void main(String args[]) {
       String dirname = "C:\\a";
       File f1 = new File(dirname);
       if(f1.isDirectory())
       {
           System.out.println("目录:"+dirname);
           String b[] = f1.list();
           for(int x=0;x
import java.awt.*;

public class iodemo
{
    public static void main(String args[])  
    {
        Frame f = new Frame("my frame");
        f.setSize(200,200);
        f.setLocation(300, 200);
        f.setVisible(true);
        f.setLayout(new FlowLayout());
        Button btn = new Button ("我是一个按键");
        f.add(btn);
        
    }
}
  1. 学生管理系统
import java.util.*;
class Stu {
    private String stuNum;//学号
    private String name;//姓名
    private String gender;//性别
    private int age;//年龄
    private int score;//分数
    public Stu() {
        super();
    }
    public String getStuNum() {
        return stuNum;
    }
    public void setStuNum(String stuNum) {
        this.stuNum = stuNum;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   
}

public class iodemo {
      static Stu[] Stu=new Stu[100];//定义学生数组,容量为100
      static  int number=0;
      static Scanner scanner=new Scanner(System.in);

    public static void main(String args[]){
      int choice;

      System.out.println("这是一个学生信息管理系统,最大容量为100,欢迎您对本系统的使用");
      while(true){
      System.out.println("1.信息添加");
      System.out.println("2.信息查看");
      System.out.println("3.信息修改");
      System.out.println("4.信息删除");
      System.out.println("5.信息按成绩从低到高排序");  
      System.out.println("6.信息查询");
      System.out.println("7.退出");
      System.out.println("请输入你要进行的操作");
      choice=scanner.nextInt();
      if(choice==7)break;
      switch(choice){
      case 1:     add(); break;//添加学生信息
      case 2:     show();break;//查询学生信息
      case 3:     change();break;//修改学生信息
      case 4:     delete();break;//删除学生信息
      case 5:     paixu();break;//对学生信息排序
      case 6:     query();break;//查询学生信息
      }       
    }
      System.out.println("感谢您对本系统的使用,欢迎下次继续使用");
    }

    static void add(){//添加学生信息
      while(true){
      System.out.println("点击任意键继续添加学生信息,停止添加输入0");
      if(scanner.nextInt()==0)break;
      System.out.println("请输入该学生的学号,姓名,性别,年龄,成绩");
      Stu[number]=new Stu();
      Stu[number].setStuNum(scanner.next());
      Stu[number].setName(scanner.next());
      Stu[number].setGender(scanner.next());
      Stu[number].setAge(scanner.nextInt());
      Stu[number].setScore(scanner.nextInt());
      number++;
      System.out.println("添加成功");
      }
     }


      static void show(){//展示学生信息
          System.out.println("*******************************************");
          System.out.println(" 学号             姓名                          性别              年龄                成绩");
          for(int i=0;iStu[j].getScore())
              {temp=Stu[i];Stu[i]=Stu[j];Stu[j]=temp;}
          }
          System.out.println("排序完成,选择”学生信息查看“进行查看");
      }


    static void change(){//改变学生信息
      System.out.println("请输入要修改信息的学生学号");
      String Stunumber=scanner.next();
      for(int i=0;i

8.读取文本
(可以读汉字)

File file = new File("C:\\a.txt");
            BufferedReader reader = new BufferedReader(new FileReader(file));
            // 构建InputStreamReader对象,编码与写入相同
     
            StringBuffer sb = new StringBuffer();
            while (reader.ready()) {
                sb.append(reader.readLine());
                // 转成char加到StringBuffer对象中
                sb.append("\r\n");
            }
            System.out.println(sb.toString());
            reader.close();

(不能读汉字)

InputStream is = new FileInputStream("C:\\a.txt") ;
            int size = is.available();
 
            for (int i = 0; i < size; i++) {
                System.out.print( is.read() + "  ");
            }
            is.close();

(某种情况可以)

 File f = new File("C:\\a.txt");
FileInputStream fip = new FileInputStream(f);
        // 构建FileInputStream对象
 
        InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
        // 构建InputStreamReader对象,编码与写入相同
 
        StringBuffer sb = new StringBuffer();
        while (reader.ready()) {
            sb.append((char) reader.read());
            // 转成char加到StringBuffer对象中
        }
        System.out.println(sb.toString());
        reader.close();
        // 关闭读取流
 
        fip.close();
        // 关闭输入流,释放系统资源

7.打开与保存

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class guidemo
{
   private Frame f;
   private TextArea ta;
   private MenuBar menuBar;
   private Menu menu;
   private MenuItem openItem,saveItem;
   private FileDialog openDialog,saveDialog;
   private File file;
    public guidemo()
    {
        init();
    }
    public void init()
    {
        f = new Frame("myFrame");
        f.setBounds(300,200,500,400);
        ta = new TextArea();
        openDialog = new FileDialog(f,"mydialog",FileDialog.LOAD);
        saveDialog = new FileDialog(f,"mydialog",FileDialog.SAVE);
        menuBar = new MenuBar();
        menu = new Menu("菜单");
        
        openItem = new MenuItem("打开");
        saveItem = new MenuItem("保存");
       
        menu.add(openItem);
        menu.add(saveItem);
        menuBar.add(menu);
        f.setMenuBar(menuBar);
        f.add(ta);
        myEvent();
        f.setVisible(true);
    }
    public void myEvent()
    {
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        
       openItem.addActionListener(new ActionListener()
               {
                     public void actionPerformed(ActionEvent e)
                     {
                         openDialog.setVisible(true);
                         String fileName = openDialog.getFile();
                         String directionName = openDialog.getDirectory();
                         if(fileName == null || directionName == null)
                             return ;
                         ta.setText("");
                         file = new File(directionName,fileName);
                         try {
                           BufferedReader buff = new BufferedReader(new FileReader(file));
                            String line = null;
                           while((line=buff.readLine())!=null)
                           {
                               ta.append(line+"\r\n");
                           }
                           buff.close();
                       } catch (FileNotFoundException e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                       } catch (IOException e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                       }
                     }
               });
       saveItem.addActionListener(new ActionListener()
       {
             public void actionPerformed(ActionEvent e)
             {
                 if(file==null)
                 {
                 saveDialog.setVisible(true);
                 String fileName = saveDialog.getFile();
                 String directionName = saveDialog.getDirectory();
                 if(fileName == null || directionName == null)
                     return ;
                 file = new File(directionName,fileName);
                 }
                 try {
                   BufferedWriter bwr = new BufferedWriter(new FileWriter(file));
                   String text = ta.getText();
                   bwr.write(text);
                   bwr.flush();
                   bwr.close();
               } catch (FileNotFoundException e2) {
                   // TODO Auto-generated catch block
                   e2.printStackTrace();
               } catch (IOException e2) {
                   // TODO Auto-generated catch block
                   e2.printStackTrace();
               }
             }
       });

       
    }
}
public class gui {
     public static void main(String []args)
     {
         new guidemo();
     }
}

8.LineNumberReader的用法
同BufferedReader相似,但又有getLineNumber和setLineNumber方法


image.png

9.UDP通信

import java.io.*;
import java.net.*;
class Sender implements Runnable
{
   public void run()
   {
       try {
           DatagramSocket ds = new DatagramSocket();
           BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
           String line = null;
           DatagramPacket dp = null;
           do {
               line = buf.readLine();
               byte []by = line.getBytes();
               dp = new DatagramPacket(by,by.length,InetAddress.getByName("127.0.0.1"),8090);
               ds.send(dp);
           }while(!line.equals("exit"));
           ds.close();
       }catch(IOException e)
       {
           e.printStackTrace();
       }
   }
}
class Receiver implements Runnable
{
   public void run()
   {
       
       try {
           DatagramSocket ds = new DatagramSocket(8090);
           byte []by = new byte[1024];
           DatagramPacket dp = new DatagramPacket(by,by.length);
           String line = null;
           do {
               ds.receive(dp);
               line = new String (dp.getData(),0,dp.getLength());
               System.out.println(line);
           }while(!line.equals("exit"));
           ds.close();
       }catch(IOException e)
       {
           e.printStackTrace();
       }
       
}

}
public class test{
    public static void main(String []args)
    {
        new Thread(new Sender()).start();
        new Thread(new Receiver()).start();
    }
}

10.TCP 上传文件

import java.net.*;
import java.io.*;
public class demo{
    public static void main(String []args) throws Exception
    {
        Socket s = new Socket("192.168.56.1",8092);
        BufferedReader bufr = 
                new BufferedReader(new FileReader("test.java"));
        PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
        BufferedReader bufin = 
                new BufferedReader(new InputStreamReader(s.getInputStream()));
        String line = null;
        while((line=bufr.readLine())!=null)
        {
            pw.println(line);
        }
        s.shutdownOutput();
        String str = bufin.readLine();
        System.out.println(str);
        bufr.close();
        s.close();
    }
}






import java.net.*;
import java.io.*;
public class send {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
      ServerSocket ss = new ServerSocket(8092);
      Socket s = ss.accept();
      String ip = s.getInetAddress().getHostAddress();
      System.out.println(ip+"      is connect");
     BufferedReader bufr = 
             new BufferedReader(new InputStreamReader(s.getInputStream()));
    PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);
    String line = null;
    while((line=bufr.readLine())!=null)
    {
        out.println(line);
    }
    PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
    pw.println("上传完成");
    out.close();
    s.close();
    ss.close();
    }

}

11.布局

         import java.awt.*; 
    public class buttonDir{ 
       public static void main(String args[]){ 
       Frame f = new Frame("BorderLayout"); 
       f.setLayout(new BorderLayout()); 
        f.add("North", new Button("North"); //一个很好的输入方法
        //第一个参数表示把按钮添加到容器的North区域 
        f.add("South", new Button("South"); 
       //第一个参数表示把按钮添加到容器的South区域 
       f.add("East", new Button("East");  
       //第一个参数表示把按钮添加到容器的East区域 
       f.add("West", new Button("West"); 
       //第一个参数表示把按钮添加到容器的West区域 
       f.add("Center", new Button("Center"); 
       //第一个参数表示把按钮添加到容器的Center区域 
       f.setSize(200,200); 
       f.setVisible(true);  
      } 
     }

你可能感兴趣的:(java 代码问题)