java小程序(3)

【文章标题】java小程序(3

【文章作者】曾健生

【作者邮箱】zengjiansheng1@126.com

【作者QQ190678908

【作者声明】本人水平有限,失误之处,敬请各位指出。本人以前写的一些小程序,分享一下希望对各位学习java有帮助 ^-^

*******************************************************************************

题目:

 

 

/*====第十七题==================================

客户端向服务端上传图片。将客户端封装成线程。

*/

 

/*====第十八题==================================

java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符。

但对应的字节数不同,一个汉字占两个字节。

定义一个方法,按照最大的字节数来取子串。

如:对于“ab你好”,如果取三个字节,那么子串就是ab与“你”字的半个,

那么半个就要舍弃。如果去四个字节就是“ab你”,取五个字节还是“ab你”.

*/

 

/*====第二十题==================================

将一个图片切割成多个文件。在将多个文件合并成该图片

 

 

/*====第二十一题==================================

编写一个方法。去除Vector集合中的重复元素。

*/

 

/*====第二十二题==================================

取出一个字符串中字母出现的次数。如:"abcdekka27qoq"  a(2)b(1)k(2)...

*/

 

 

 

 

 

 

/*====第十七题==================================

客户端向服务端上传图片。将客户端封装成线程。

*/

 

import java.net.*;

import java.io.*;

 

//服务端程序

class ServerJpg

{

       public static void main( String args[] ) throws Exception

       {

              ServerSocket ser =new ServerSocket( 9999 );

              Socket s=ser.accept();

              InputStream in=s.getInputStream();

              byte buf[]=new byte[1024];

              int num=0;

              //写入到一个JPG文件中

              FileOutputStream fis=new FileOutputStream( "2.jpg" );

              while( ( num=in.read(buf) )!=-1 )

              {

                     fis.write( buf, 0, num );

              }

              s.close();

              ser.close();

              in.close();

              fis.close();

       }

}

 

//客户端程序,封装成一个线程

class clientJpg implements Runnable

{

       public void run()

       {

              Socket s=null;

              OutputStream os=null;

              FileInputStream fis=null;

              try

              {

                     s=new Socket( "10.1.15.124", 9999 );

                     os=s.getOutputStream();

                     fis=new FileInputStream( "1.jpg" );

                     byte buf[]=new byte[1024];

                     int num=0;

                     while( (num=fis.read(buf))!=-1 )

                     {

                            os.write( buf,0, num );

                     }

              }

              catch( Exception e )

              {

                     e.printStackTrace();

              }

              finally

              {

                     try

                     {

                            s.shutdownInput();

                            os.close();

                            fis.close();

                     }

                     catch( Exception e )

                     {

                            e.printStackTrace();

                     }

              }

       }

}

 

class Demo

{

       public static void main(String args[])

       {

              new Thread( new clientJpg()).start();

       }

}

 

 

/*====第十八题==================================

java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符。

但对应的字节数不同,一个汉字占两个字节。

定义一个方法,按照最大的字节数来取子串。

如:对于“ab你好”,如果取三个字节,那么子串就是ab与“你”字的半个,

那么半个就要舍弃。如果去四个字节就是“ab你”,取五个字节还是“ab你”.

 

思路:

首先要知道汉字是由连个字节所组成。而且这两个都是负数。

1,先将该字符串变成字节数组(getBytes

2,通过要截取的长度位。来判断该位置是正数还是负数。

       只要为负数。就往回判断前一个字节是正还是负。以此类推。

       定义一个计数器,记录负数出现的次数。如果该数为偶数。那么就将要去长度的字节数。转成字符串(说明没有半个汉字存在。)

       如果是奇数。就将最后一个字节舍弃。在转成字符串。

*/

 

import java.io.*;

 

class Demo

{

       public static void main(String args[])

       {

              byte buf[]="ab你好".getBytes();

              int num=0;//记录是第几个数字

              int count=0;    //记录负数的个数

             

              try

              {

                     BufferedReader br=new BufferedReader(

                            new InputStreamReader(System.in));

                    

                     num=Integer.parseInt( br.readLine() );

              }

              catch( Exception e )

              {

                     e.printStackTrace();

                     System.out.println( "输入有误" );

                     return;                  

              }

             

              for(int i=0; i<num-1; i++ )

              {

                     if( buf[i]<0 )

                     {

                            count++;

                     }

              }

             

              if( count%2==0 )

              {

                     System.out.println( num );

                     System.out.println( new String( buf,0, num-1 ));

              }           

              else

              {

                     System.out.println( num );

                     System.out.println( new String( buf,0, num ));

              }

       }

      

 

}

 

 

 

/*====第十九题==================================

利用LinkedList去实现一个队列的效果.

自定该功能类.(队列的特点是元素先进先出,去查看LinkList中的方法)

*/

class DuiLie<T>

{

       private LinkedList<T> ll;

       DuiLie()

       {

              ll = new LinkedList<T>();

       }

       public void myAdd(T t)

       {

              ll.addFirst(t);

       }

       public T myGet()

       {

              return ll.removeLast();

       }

       public boolean isNull()

       {

              return ll.isEmpty();

       }

}

 

class TD

{

       public static  void main(String[] args)

       {

              DuiLie dl = new DuiLie();

              dl.myAdd("hahah");

              dl.myAdd("kkkk");

              dl.myAdd("qqq");

              while(dl.isNull())

              {

                     System.out.println(dl.myGet());

              }

       }

}

/*====第二十题==================================

 

 

将一个图片切割成多个文件。在将多个文件合并成该图片

 

 

1,通过字节流读取该图片。定义一个缓冲区数组该数组的大小 是要生成的切割后的文件大小。

通过输出流将该数组中的数据,写到一个文件中(文件名要有规律。1.haha  2.haha...

(可以通过一个自定义配置文件保存原文件的基本属性信息。如:源文件的名字 已经切割出来的碎片文件的个数。方便合并。)

 

2,合并,首先要知道碎片文件的目录,列出该目录当前的所有.haha文件。(遍历时可以通过配置文件中的信息确定循环的次数。)

并按照顺序把每一个碎片文件用流读取。(一个文件对应一个流。)

将这些流存入集合。(why?因为要通过序列流进行合并。(SequenceInputStream)该流会接受一个Enumeration

 

3,读取序列流中的数据,并把该数据都写入到一个(图片)文件中。

*/

 

import java.io.*;

 

/*文件分割类,

1.通过流的形式读取源数据

2.通过定义一个固定的字节数组,实现文件分割

  a,用FileInputStream读取源文件

  b,通过一个字节数组作为中介存储

  c,把读取到的数据写入到一个新文件

3.把原来的文件名和分割的个数写入到一个配置文件中

*/

 

import java.io.*;

 

/*文件分割类,

1.通过流的形式读取源数据

2.通过定义一个固定的字节数组,实现文件分割

  a,用FileInputStream读取源文件

  b,通过一个字节数组作为中介存储

  c,把读取到的数据写入到一个新文件

3.把原来的文件名和分割的个数写入到一个配置文件中

*/

import java.io.*;

 

/*文件分割类,

1.通过流的形式读取源数据

2.通过定义一个固定的字节数组,实现文件分割

  a,用FileInputStream读取源文件

  b,通过一个字节数组作为中介存储

  c,把读取到的数据写入到一个新文件

3.把原来的文件名和分割的个数写入到一个配置文件中

*/

 

import java.io.*;

import java.util.*;

 

/*文件分割类,

1.通过流的形式读取源数据

2.通过定义一个固定的字节数组,实现文件分割

  a,用FileInputStream读取源文件

  b,通过一个字节数组作为中介存储

  c,把读取到的数据写入到一个新文件

3.把原来的文件名和分割的个数写入到一个配置文件中

*/

class FileSplit

{

       private File f;

       private FileInputStream fis;

       private FileOutputStream fos;

       private String fileName;

       int count;

      

       //传入要分割的文件路径

       FileSplit( String s )

       {

              f=new File( s );

              fileName=s;

              count=0;

       }

      

       public void split()

       {

              try

              {                  

                     fis=new FileInputStream( f );

                     byte buf[]=new byte[1024*256];

                     int num=0;

                     while( ( num=fis.read( buf ) )!=-1 )

                     {

                            //创建一个临时的分割文件对象

                            if( createSplitFile( buf, 0, num )==-1 )

                            {

                                   return;

                            }

                            count++;                                                        

                     }                                                     

              }

              catch( IOException e )

              {

                     e.printStackTrace();

              }

              finally

              {

                     //关闭输入流

                     if( fis!=null )

                     {

                            try

                            {

                                   fis.close();

                            }

                            catch( IOException e )

                            {

                                   e.printStackTrace();

                            }

                     }

              }

             

              //创建配置文件“file.ini”,其中格式为“文件个数>文件名”

              createInfoFile();

             

              System.out.println( "文件分割成功" );

       }

      

       创建一个临时的分割文件对象,如果返回-1表示创建失败,

       //程序退出

       private int createSplitFile( byte buf[],int zero,int num )

       {

              //创建临时的文件对象

              FileOutputStream fosTemp=null;

              try

              {

                     fosTemp=new FileOutputStream( count+".haha" );

                     fosTemp.write( buf,zero,num );

                     fosTemp.flush();

              }

              catch( IOException e )

              {

                     System.out.println( "文件"+count+".haha创建失败" );

                     return -1;

              }

              finally

              {

                     //关闭输出流

                     try

                     {

                            fosTemp.close();

                     }

                     catch( IOException e )

                     {

                            e.printStackTrace();

                     }

              }

              return 1;        

       }

      

       //创建配置文件“file.ini”,其中格式为“文件个数>文件名”

       private void createInfoFile()

       {

              File infoFile=new File( "file.ini" );

              BufferedWriter br=null;

              try

              {

                     //如果文件不存在就创建一个新的的文件

                     if( !infoFile.exists() )

                     {

                            infoFile.createNewFile();

                     }

             

                     br=new BufferedWriter(

                            new FileWriter(infoFile) );

                     br.write( count+">"+fileName );

                     br.newLine();

                     br.flush();

              }

              catch( IOException e )

              {

                     e.printStackTrace();

              }

              finally

              {

                     if( br!=null )

                     {

                            try

                            {

                                   br.close();

                            }

                            catch( IOException e )

                            {

                                   e.printStackTrace();

                            }

                     }

              }

       }

      

}

 

/*

文件合并类

1.通过枚举获取配置文件对象

2.通过输入流获取文件个数和源文件名称

3.把各个文件对象存放在序列流中

4.把通过序列流合并成一个文件

*/

class FileMerge

{

       File fileDir; //配置文件目录

      

       FileMerge( String s )

       {

              this.fileDir=new File( s );

       }

      

      

       public void startMerge()

       {

              //获取配置文件对象

              File f=getFile( fileDir );

             

              //获取文件的配置信息

              String fileInfo=getFileInfo( f );

             

              //获取文件的个数

              int count=getFileCount( fileInfo );

             

              //获取分割前的文件名

              String fileName=getFileName( fileInfo );

             

              //获取枚举集合

              Vector<FileInputStream> v=getAllFile( count );

             

              //如果集合不为空就合并文件

              if( v!=null  )

              {

                     merge( v,fileName );

              }

       }

      

       //获取配置文件对象

       private File getFile( File fileDir )

       {

              File fileList[]=fileDir.listFiles();

              for( int i=0; i<fileList.length; i++ )

              {

                     if( fileList[i].getName().equals("file.ini"))

                     {

                            return fileList[i];

                     }

              }

              return null;

       }

      

       //获取文件的配置信息

       private String getFileInfo( File f )

       {

             BufferedReader br=null;

             String s=null;

            

             try

             {

                    //读取配置信息

                    br=new BufferedReader( new FileReader( f ) );

                    s=br.readLine();

             }

             catch( IOException e )

             {

                    System.out.println( "读取配置文件失败" );

                    e.printStackTrace();

             }

             finally

             {

                    //关闭输入流

                    if( br!=null )

                    {

                           try

                           {

                                  br.close();

                           }

                           catch( IOException e )

                           {

                                  e.printStackTrace();

                           }

                    }

             }

            

             return s;

       }

      

       //获取文件的个数

       private int getFileCount( String fileInfo )

       {

              String num=null;

              if( fileInfo!=null )

              {

                     num=fileInfo.substring( 0,fileInfo.indexOf('>') );

              }

             

              return Integer.parseInt(num) ;

       }

             

       //获取分割前的文件名

       private String getFileName( String fileInfo )

       {

              String fileName=null;

              if( fileInfo!=null )

              {

                     fileName=fileInfo.substring( fileInfo.indexOf('>')+1 );

              }

             

              return fileName;

       }    

      

       //获取枚举集合

       private     Vector<FileInputStream> getAllFile( int count )

       {

              Vector<FileInputStream> v=new Vector<FileInputStream>();

              for( int i=0; i<count; i++ )

              {

                     File f=null;

                     try

                     {

                            f=new File( fileDir, i+".haha" );

                            if( !f.exists() )

                            {

                                   System.out.println( i+".haha文件不存在,合并失败" );

                                   return null;

                            }

                            v.add(new FileInputStream( f ));

                     }

                     catch( IOException e )

                     {

                            e.printStackTrace();

                     }

                    

                    

              }

              return v;

       }

      

       //用序列流合并文件

       //V为枚举接口,count为文件数

       private void merge( Vector<FileInputStream> v ,String fileName )

       {

              Enumeration<FileInputStream> e=v.elements();

              SequenceInputStream sis=new SequenceInputStream(e);

              FileOutputStream fos=null;

              byte buf[]=new byte[1024];

             

              try

              {

                     //输出到文件fileName

                     fos=new FileOutputStream( fileName );

                     int num=0;

                    

                     //读取文件的内容

                     while( (num=sis.read(buf))!=-1 )

                     {

                            fos.write( buf,0,num );

                            fos.flush();

                     }

              }

              catch( IOException e1 )

              {

                     e1.printStackTrace();

              }

              finally

              {

                     //关闭流

                     try

                     {

                            sis.close();

                            fos.close();

                     }

                     catch( IOException e1 )

                     {

                            e1.printStackTrace();

                     }

              }

             

       }

             

}

 

class Demo

{

       public static void main(String arsg[])

       {

              //分割文件

              new FileSplit( "1.bmp" ).split();

             

              //合并文件

              new FileMerge( "e://test" ).startMerge();

       }

}

 

 

/*====第二十一题==================================

编写一个方法。去除Vector集合中的重复元素。

思路:

1,自定义一个Vector集合。对原有的Vector集合进行迭代。

把迭代出来的元素存入到新集合中。在存的时候进行判断新的集合中是否有该元素。‘

如果有,就不要进行存储。

迭代完成 。新的Vector集合中就不存在重复元素了。

*/

 

import java.util.*;

class Demo

{

       public static void main( String args[] )

       {

              Vector<Integer> v=new Vector<Integer>();

              Vector<Integer> ve=new Vector<Integer>();

              int num=0;

              v.add( 45 );

              v.add( 45 );

              v.add( 46 );

              v.add( 49 );

             

              Enumeration<Integer> e=v.elements();

              while( e.hasMoreElements() )

              {

                     num=e.nextElement();

                     if( !( ve.contains( num )) )

                     {

                            ve.add( num );

                     }

              }

             

              Enumeration<Integer> e1=ve.elements();

              while( e1.hasMoreElements() )

              {

                     System.out.println( e1.nextElement() );

              }

             

             

       }

}

 

 

/*====第二十二题==================================

取出一个字符串中字母出现的次数。如:"abcdekka27qoq"  a(2)b(1)k(2)...

思路:

1,先将字符串转换成字符数组。

2,定义一个计数器,记录每一个字母出现的次数。

3,将字母作为key。该字母出现的次数作为value。将两者存入map集合中。

(为什么要用map集合呢?因为字母有很多,数据多了就要想到用容器多存入。方便操作。而每一个字母都有对应的次数。正好有映射关系。而map集合正好符合这个特点。map集合可以同时存入两个对象。key会保证唯一性。只要不断改变该key对应的值就可以了。)

4,在遍历字符数组进行存储的时候要判断,该字母是否已经存在于map集合。如果已存在。就取出它所对应的次数,并++.在次存入到集合中。

5,遍历该集合就可以得到结果。

 

(注意,如果只取字母的话,要进行字符'a'-'z'的判断。其他字符比如数字或者符号就没有必要存入map集合了。)

*/

 

import java.util.*;

import java.io.*;

class CountMaxNum

{

       private String s;

       private HashMap<Character,Integer> map;

       CountMaxNum( String s )

       {

              this.s=s;

              map=new HashMap<Character,Integer>();

       }

      

       public void start()

       {

              inputSet();

              printString();

       }

      

       /*

       利用了map容器"不允许重复元素""键和值一一对应"的特点

       1.遍历字符串中的每个元素

       2.检查遍历到的元素是否已在map容器中

       3.如果元素不在容器中,初始化出现次数为1,把"元素,出现次数"放入容器中

       4.如果元素在容器中,把出现的次数取出来加1后再把"元素,出现次数"放入容器中

       */

       private void inputSet()

       {

              for( int i=0; i<s.length(); i++ )

              {

                     if( (s.charAt(i)>='a'&&s.charAt(i)<='z')

                            ||(s.charAt(i)>='A'&&s.charAt(i)<='Z') )

                     {

                            //如果元素在容器中,把出现的次数取出来加1后再把"元素,出现次数"放入容器中

                            if( map.containsKey( s.charAt(i) ))

                            {

                                   map.put( s.charAt(i), map.get( s.charAt(i) )+1 );

                            }

                            else //元素不在容器中,初始化出现次数为1,把"元素,出现次数"放入容器中

                            {

                                   map.put( s.charAt(i),1 );

                            }

                     }

              }

       }

      

       private void printString()

       {

              Iterator<Map.Entry<Character,Integer>> iter=map.entrySet().iterator();

              while( iter.hasNext() )

              {

                     Map.Entry<Character,Integer> i=iter.next();

                     System.out.println( i.getKey()+":"+i.getValue() );

              }

       }

}

 

class Demo

{

       public static void main(String agrs[])

       {

              new CountMaxNum( "abcdekka27qoq" ).start();        

       }

}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

newjueqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值