主题:Java应用基础:25个精选试题分享[4]
http://www.educity.cn 作者:佚名 来源:希赛教育
19. 编程题:列出某文件夹下的所有文件(文件夹从命令行输入)。
解:
import Java.io.*;
public class listFile
{
public static void main (String[] args)
{
String s="";
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
try {
s = in.readLine();
File f=new File(s);
File[] files=f.listFiles();
for(int i=0;i
{
if(files[i].isFile())
{
System.out.println("文件:"+files[i]);
}
else
{
System.out.println("目录:"+files[i]);
}
}
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
20. 编程题:写一个满足Singleton模式的类出来。
public class SingletonTest
{
private static SingletonTest sp;
private SingletonTest() {}
public static SingletonTest getInstance()
{
if (sp==null)
{ sp=new SingletonTest(); }
return sp;
}
21. 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
解:
import Java.io.*;
class interceptString
{
String interceptStr;
int interceptByte;
public interceptString(String str,int bytes)
{
interceptStr=str;
interceptByte=bytes;
System.out.println("字符串为:'"+interceptStr+"';字节数为:"+interceptByte);
}
public void interceptIt()
{
int interceptCount; interceptCount=(interceptStr.length()%interceptByte==0)?(interceptStr.length()/interceptByte):(interceptStr.length()/interceptByte+1);
System.out.println("截取后断数为:"+interceptCount);
for (int i=1;i<=interceptCount ;i++ )
{ if (i==interceptCount)
{
System.out.println(interceptStr.substring((i-1)*interceptByte,interceptStr.length()));
} else
{
System.out.println(interceptStr.substring((i-1)*interceptByte,(i*interceptByte)));
}
}
}
public static void main(String[] args)
{
String s="";
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
try {
s = in.readLine();
interceptString ss = new interceptString(s,4);
ss.interceptIt();
in.close();
} catch (IOException e)
{ e.printStackTrace();}
}
}
19. 编程题:列出某文件夹下的所有文件(文件夹从命令行输入)。
解:
import Java.io.*;
public class listFile
{
public static void main (String[] args)
{
String s="";
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
try {
s = in.readLine();
File f=new File(s);
File[] files=f.listFiles();
for(int i=0;i
{
if(files[i].isFile())
{
System.out.println("文件:"+files[i]);
}
else
{
System.out.println("目录:"+files[i]);
}
}
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
20. 编程题:写一个满足Singleton模式的类出来。
public class SingletonTest
{
private static SingletonTest sp;
private SingletonTest() {}
public static SingletonTest getInstance()
{
if (sp==null)
{ sp=new SingletonTest(); }
return sp;
}
21. 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
解:
import Java.io.*;
class interceptString
{
String interceptStr;
int interceptByte;
public interceptString(String str,int bytes)
{
interceptStr=str;
interceptByte=bytes;
System.out.println("字符串为:'"+interceptStr+"';字节数为:"+interceptByte);
}
public void interceptIt()
{
int interceptCount; interceptCount=(interceptStr.length()%interceptByte==0)?(interceptStr.length()/interceptByte):(interceptStr.length()/interceptByte+1);
System.out.println("截取后断数为:"+interceptCount);
for (int i=1;i<=interceptCount ;i++ )
{ if (i==interceptCount)
{
System.out.println(interceptStr.substring((i-1)*interceptByte,interceptStr.length()));
} else
{
System.out.println(interceptStr.substring((i-1)*interceptByte,(i*interceptByte)));
}
}
}
public static void main(String[] args)
{
String s="";
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
try {
s = in.readLine();
interceptString ss = new interceptString(s,4);
ss.interceptIt();
in.close();
} catch (IOException e)
{ e.printStackTrace();}
}
}