java写文件流(java写入流)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
本篇文章给大家谈谈java写文件流,以及java写入流对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
java写文件流返回线程超时
java写文件流返回线程超时可以进行中断处理,包括中断线程并且返回超时的结果,有时候线程已胡缺瞎经在执行了,是无法中断的,程序要返回超时的结果。Java是一门面向对象编程语言,1990年代初裤空由詹姆斯·高斯林等人开发出Java语言的雏形,最初被命名为Oak,后随着扮闹互联网的发展,经过对Oak的改造,1995年5月Java正式发布。

java中的文件流
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test implements ActionListener {
String[] list1;
String[] list2;
String file1 = "/home/soft01/1.txt";//1.txt路径
String file2 = "/home/soft01/2.txt";//2.txt路径
JTextField jtf1;
JTextField jtf2;
int num = 3;
private boolean flag = true;
public Test() {
begin();
list1 = fileToString(file1);
list2 = fileToString(file2);
}
public void begin() {
JFrame jf = new JFrame();
jf.setLayout(new GridLayout(3, 2));
JLabel jl1 = new JLabel(" 1");
JLabel jl2 = new JLabel(" 2");
JLabel jl3 = new JLabel(" res");
jtf1 = new JTextField(8);
jtf2 = new JTextField(8);
JTextField jtf3 = new JTextField(8);
jtf3.addActionListener(this);
jf.add(jl1);
jf.add(jtf1);
jf.add(jl2);
jf.add(jtf2);
jf.add(jl3);
jf.add(jtf3);
jf.pack();
jf.setLocation(400, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public String[] fileToString(String filePath) {
String text = "";
try {
String str;
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath)));
while ((str = reader.readLine()) != null) {
text = text + str + " ";
}
} catch (Exception e) {
e.printStackTrace();
}
return text.split(" ");
}
public void actionPerformed(ActionEvent e) {
if (num == list1.length) {
num = 3;
}
if (flag) {
jtf1.setText(list1[num]);
} else {
jtf2.setText(list2[num]);
num++;
}
flag = !flag;
}
public static void main(String[] args) {
new Test();
}
}
希望分数给我,写这个也不容易了。
mac系统,java编程中文件流的路径是如何写的
看看这个,我昨天刚写的:早弯岁 import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;public class AddList {
private String filePath = "";
private String bakPath = ""闹漏;
private String content = "";
Scanner sc = new Scanner(System.in);
public String readFile(){
content = "";
if (isNull(filePath)) {
System.out.println("文件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileReader fr = null;
try {
if (file.exists()) {
fr = new FileReader(file);
char[] chars = new char[1024];
int n = 0;
while((n = fr.read(chars)) != -1){
String string = new String(chars, 0, n);
content = content + string;
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
}
public void writeFile(String path){
File file = new File(path);
FileOutputStream fos = null;
mkDirs(path);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void writeFile(){
if (isNull(filePath)) {
System.out.println("文陆睁件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileOutputStream fos = null;
mkDirs(filePath);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void mkDirs(String filepath){
if (filepath.indexOf("\\") != -1) {
filepath = filepath.replaceAll("\\", "/");
}
int n = filepath.indexOf("//");
String path = filepath.substring(0, n) + "//";
filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length());
String[] files = filepath.split("/");
for (int i = 0; i files.length - 1; i++) {
path = path + files[i];
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}
}
public void addImfor(){
System.out.println("--------增加记录---------");
String name = "";
String tel = "";
String email = "";
content = readFile();
while(true){
System.out.println("姓名:");
name = sc.next();
System.out.println("电话:");
tel = sc.next();
System.out.println("Email:");
email = sc.next();
content = content + name + "" + tel + "" + email +"==";
System.out.println("0、Exit 1、继续");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
writeFile();
}
public void deleteImfor(){
System.out.println("---------删除记录---------");
String name = "";
String[] imfors = null;
content = readFile();
while(true){
System.out.println("你要删除的姓名是:");
name = sc.next();
if (content.indexOf(name) != -1) {
imfors = content.split("==");
for (int i = 0; i imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
content = "";
for (int i = 0; i imfors.length; i++) {
if (!isNull(imfors[i])) {
content = content + imfors[i] + "==";
}
}
writeFile();
System.out.println("删除成功");
} else {
System.out.println("此人不存在");
}
System.out.println("0、Exit 1、继续");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
}
public void viewAll(){
System.out.println("----------显示所有------------");
content = readFile();
if (!isNull(content)) {
String[] imfors = content.split("==");
System.out.println("姓名\t电话\tEmail");
for (int i = 0; i imfors.length; i++) {
String[] imfor = imfors[i].split("");
for (int j = 0; j imfor.length; j++) {
System.out.print(imfor[j] + "\t");
}
System.out.println();
}
} else {
System.out.println("暂时还没有记录");
}
}
public void queryImfor(){
System.out.println("----------查找记录-----------");
content = readFile();
if (!isNull(content)) {
String result = "";
String[] imfors = null;
String[] imfor = null;
String name = "";
boolean bool = false;
while(true){
result = "";
System.out.println("请输入关键字(按姓名查找):");
name = sc.next();
bool = false;
if (content.indexOf(name) != -1) {
imfors = content.split("==");
for (int i = 0; i imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfor = imfors[i].split("");
if (imfor[0].equals(name)) {
bool = true;
result = result + imfors[i] + "==";
}
}
}
if (bool) {
imfors = result.split("==");
System.out.println("姓名\t电话\tEmail");
for (int i = 0; i imfors.length; i++) {
imfor = imfors[i].split("");
for (int j = 0; j imfor.length; j++) {
System.out.print(imfor[j] + "\t");
}
System.out.println();
}
} else {
System.out.println("无此人信息");
}
} else {
System.out.println("无此人信息");
}
System.out.println("0、Exit 1、继续");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
} else {
System.out.println("文件还没有记录");
}
}
public void copy(){
System.out.println("----------备份-----------");
content = readFile();
if (isNull(bakPath)) {
System.out.println("备份全路径:");
bakPath = sc.next();
}
writeFile(bakPath);
System.out.println("备份成功");
}
public boolean isNull(String string){
if (null == string || "" == string || 0 == string.length()) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
AddList add = new AddList();
Scanner sc = new Scanner(System.in);
int operater = 0;
while(true){
System.out.println("选择功能:\n1、增加记录 2、删除记录 3、显示所有 4、查询记录 5、备份 6、退出");
operater = sc.nextInt();
if (1 == operater) {
add.addImfor();
} else if (2 == operater) {
add.deleteImfor();
} else if (3 == operater) {
add.viewAll();
} else if (4 == operater) {
add.queryImfor();
} else if (5 == operater) {
add.copy();
} else if (6 == operater) {
System.out.println("谢谢使用");
break;
}
}
}
}
java写文件流的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java写入流、java写文件流的信息别忘了在本站进行查找喔。
