习题3:
完成下面方法中的代码,要求建立一个缓冲区,将字节输入流中的内容转为字符串。
static String loadStream(InputStream in) throws IOException {…}
代码:
public class test {
static String loadStream(InputStream in) throws IOException {
StringBuffer buffer = new StringBuffer();
int n = 1,i=5;
byte[] buf = new byte[n]; // 缓冲区,大小为1字节
while(i>0) {
in.read(buf,0,n);
System.out.println(new String(buf));
buffer.append(new String(buf));
i--;
}
return new String(buffer);
}
public static void main(String[] args) throws IOException {
InputStream in = new BufferedInputStream(System.in);//in必须要初始化一个对象,不能为null
String test = loadStream(in);
System.out.println("result = " + test);
}
}
结果:
习题4:
编写程序,将一个字符串转为字节数组输入流,将这个流中所有小写字母换成大写字母并写入字节数组输出流中,再将数组输出流转为字符串。
代码:
public class test {
public static void main(String[] args) {
String str = "Hello World!"; // 初始字符串
ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes()); // 字节数组输入流
ByteArrayOutputStream os = new ByteArrayOutputStream(); // 字节数组输出流
int b = -1;
while((b=is.read()) != -1){
System.out.print(" "+(char)b);
if(b >= 97 && b <= 122){ // 大写字母65~90, 小写字母 97~122
b -= 32;
}
os.write(b);
}
System.out.print("\nresult = ");
String out = os.toString();
System.out.println(out);
}
}
结果:
习题5:
完成下面方法中的代码,方法中的参数为一个文本文件的名称,要求将文件中的内容转为字符串。
static public String loadFile(String filename){…}
代码:
public class test {
static public String loadFile(String filename) {
File file = new File(filename);
try {
Reader readf = new FileReader(file);
long len = file.length();
char[] ch = new char[(int)len]; // 一次全部读入
readf.read(ch);
readf.close();
return new String(ch); // 变为字符串
} catch(IOException ioe) {
return null;
}
}
public static void main(String[] args) {
String fname = "test.txt";
String test = loadFile(fname);
System.out.println(test);
}
}
习题6:
完成下面方法中的代码,将字符串contents中的内容写入文件filename中。
static public boolean saveFile(String filename,String contents){…}
代码:
public class test {
static public boolean saveFile(String filename, String contents) {
try {
File file = new File(filename);
if(!file.exists()) {
file.createNewFile();
}
Writer writer = new FileWriter(file);
char[] ch = contents.toCharArray(); // 字符串-->字符数组
writer.write(ch);
writer.close();
return true;
} catch(IOException ioe) {
return false;
}
}
public static void main(String[] args) throws IOException {
String fname = "test.txt";
String contents = "hello world!";
if(saveFile(fname,contents))System.out.println("successful!");
}
}
习题7:
socket套接字有一个方法getInputStream()
,其含义是得到从网络上传过来的数据流。现要求编写一段程序,将接收的数据存入文件。
代码:
该代码已在Java实验大作业“java聊天器文件传输”功能上实现,这里就不再重复写了。
习题8:
编写程序实现文件查找功能。提供两个参数,第一个参数为查找的起始路径,第二个参数为要查找的文件名称。如果找到,则给出文件的完整路径,否则提示文件不存在。
代码:
public class test {
String path = null;
public String filter(File dir, String fileName) {
if(dir.isFile()) { // 如果dir是一个文件
if(dir.getName().equals(fileName))
path = dir.getAbsolutePath();
}
if (dir.isDirectory()) { // 如果dir是一个目录
File[] files = dir.listFiles();
File f ;
for(int i=0;i<files.length;++i) {
f = files[i];
path = filter(f,fileName); // 递归调用,处理多层文件夹情况
}
}
return path;
}
public static void main(String[] args) {
File dir = new File("C:\\Users\\lenovo\\Desktop\\Java");
String fileName = "Java程序设计课程设计-聊天室实验指导书.docx";
if (!dir.exists()){
System.out.println("目录不存在:" + dir);
System.exit(0);
}
String path = new test().filter(dir,fileName);
if(path!=null)
System.out.println("文件完整路径:" + path);
else
System.out.println("不存在此文件");
}
}
结果:
p252 习题6:
利用串行化技术和Socket通信,将一个客户端构造的对象传送到服务器端,并输出该对象的属性值。
代码:
同上面第7题,该题代码也在Java实验大作业“java聊天器文件传输”功能中运用到了,这里就不再重复写了。