字节写入
public static void main(String[] args) throws Exception {
//读取远程文件
InputStream input = new URL("http://127.0.0.1:8080/20200818.txt").openStream();
//字节写入
FileOutputStream out =new FileOutputStream("E:\\root\\test.txt");
int len=-1;
byte[] b=new byte[1024];
while((len=input.read(b))!=-1){
out.write(b,0,len);
}
out.close();
input.close();
}
行写入
public static void main(String[] args) throws Exception {
//远程文件读取
InputStream input = new URL("http://127.0.0.1:8080/20200818.txt").openStream();
BufferedReader in2 = new BufferedReader(new InputStreamReader(input, Charset.forName("utf-8")));
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\root\\"+DateUtils.getYesterday()+".txt"), Charset.forName("UTF-8")));
String str ;
while ((str =in2.readLine()) !=null){
writer.write(str);
writer.newLine();
}
in2.close();
writer.close();
}
读取本地文件
@Data
public class User{
/**
* 主键
*/
@TableId(type = IdType.UUID)
private String id;
/**
* 创建时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createdTime;
/**
* 是否有效
*/
private Integer deleteFlag = 0;
/**
* 更新时间
*/
@TableField(value = "UPDATED_TIME", update = "SYSDATE")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updatedTime;
/**
* user代码
*/
@Excel(name = "user代码")
private String userCode;
/**
*user名称
*/
@Excel(name = "user名称")
private String userName;
/**
* 团队代码
*/
@Excel(name = "团队代码")
private String groupCode;
/**
* 团队名称
*/
@Excel(name = "团队名称")
private String groupName;
}
@Autowired
private UserMapper userMapper;
@Test
public void importGroup() throws Exception {
ImportParams params = new ImportParams();
//表格标题行数,默认0
params.setTitleRows(0);
//表头行数,默认1
params.setHeadRows(1);
//是否需要校验上传的Excel
params.setNeedVerfiy(true);
File file = new File("F:\\import\\user.xlsx");
FileInputStream fileInputStream = new FileInputStream(file);
List<User> users = ExcelImportUtil.importExcel(fileInputStream, User.class, params);
for (User user: users ) {
user.setId(UUIDUtils.getId());
user.setDeleteFlag(0);
user.setCreatedTime(new Date());
user.setUpdatedTime(new Date());
userMapper.insert(user);
}
}