1.上传图片到当前项目中(使用字节缓冲输入输出流)
package Stream.code;
import java.io.*;
public class uploadpictures {
public static void main(String[] args) {
try (
InputStream imgis = new BufferedInputStream(new FileInputStream("C:\\Users\\23298\\Desktop\\img.png"));
OutputStream imgos = new BufferedOutputStream(new FileOutputStream("F:\\ideafile\\javaTest10\\src\\Stream\\lib\\img.png"))
) {
byte[] buffer = new byte[256];
int len;
while ((len = imgis.read(buffer)) != -1){
imgos.write(buffer, 0 , len);
}
System.out.println("上传成功!");
} catch (Exception e){
e.printStackTrace();
}
}
}
2.上传一个记事本文件到当前项目中(使用字符文件输入输出流)
package Stream.code;
import java.io.*;
public class uploadfile {
public static void main(String[] args) {
try (
InputStream fileInputStream = new FileInputStream("C:\\xxx\\xxx\\Desktop\\file.txt");
OutputStream fileOutputStream= new FileOutputStream("F:\\ideafile\\javaTest11\\src\\Stream\\lib\\file.txt")
) {
byte[] buffer = new byte[256];
int len;
while ((len = fileInputStream.read(buffer)) != -1){
fileOutputStream.write(buffer, 0 , len);
}
System.out.println("上传成功!");
} catch (Exception e){
e.printStackTrace();
}
}
}
3.添加5个学生的姓名到name.txt文件中(使用集合的方式用字符缓冲输入输出流)
package Stream.code;
import java.io.*;
import java.util.ArrayList;
public class AddInformation {
public static void main(String[] args) throws Exception {
File file = new File("F:\\ideafile\\javaTest11\\src\\Stream\\lib\\name.txt");
file.createNewFile();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file.getAbsolutePath(), true));
ArrayList<String> list = new ArrayList<>();
list.add("碳木子");
list.add("AAA");
list.add("BBB");
list.add("CCC");
list.add("DDD");
for (String name : list) {
bufferedWriter.write(name);
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedWriter.close();
BufferedReader bufferedWriterreader = new BufferedReader(new FileReader(file.getAbsolutePath()));
String line;
while ((line = bufferedWriterreader.readLine()) != null) {
System.out.println(line);
}
bufferedWriter.close();
}
}
4.点名器:在第3个实验中生成一个name.txt文件,里面存储了班级同学的姓名,每个姓名占一行,要求通过程序实现随机点名器。
package Stream.code;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class RollCall {
public static void main(String[] args) {
try(
BufferedReader bufferereader = new BufferedReader(new FileReader("F:\\ideafile\\javaTest10\\src\\Stream\\lib\\name.txt"));
MyConnection connection = new MyConnection();
) {
ArrayList<String> list =new ArrayList<>();
String line;
while ((line = bufferereader.readLine()) != null) {
list.add(line);
}
Random random =new Random();
System.out.println("点名器:\n"+list.get(random.nextInt(0, list.size())));
}catch (Exception e){
e.printStackTrace();
}
}
}
class MyConnection implements AutoCloseable{
@Override
public void close() throws IOException {
System.out.println("连接资源被成功释放了!");
}
}
5.添加一个用户信息到文件(使用对象输出输入流)
package Stream.code;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class AddUserInformation {
public static void main(String[] args) {
User user = new User("木子锌");
try (FileOutputStream outputStream = new FileOutputStream("F:\\ideafile\\javaTest11\\src\\Stream\\lib\\name.txt",true)) {
outputStream.write(user.toString().getBytes(StandardCharsets.UTF_8));
System.out.println("保存成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.添加多个用户信息到文件(采用集合并使用对象输出输入流)
package Stream.code;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class AddArrylistInformstion {
public static void main(String[] args) {
ArrayList<User> list =new ArrayList<>();
AddArrylistInformstion addArrylistInformstion =new AddArrylistInformstion();
list.add(addArrylistInformstion.usermethod("ABC"));
list.add(addArrylistInformstion.usermethod("ABD"));
list.add(addArrylistInformstion.usermethod("ABE"));
list.add(addArrylistInformstion.usermethod("ABF"));
list.add(addArrylistInformstion.usermethod("ABQ"));
try (FileOutputStream outputStream = new FileOutputStream("F:\\ideafile\\javaTest11\\src\\Stream\\lib\\name.txt",true)) {
for (User user:list){
outputStream.write(user.toString().getBytes(StandardCharsets.UTF_8));
outputStream.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
}
System.out.println("保存成功");
} catch (IOException e) {
e.printStackTrace();
}
}
public User usermethod(String name){
User user =new User(name);
return user;
}
}