java io流对文件的增删改查

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java io流对文件的增删改查相关的知识,希望对你有一定的参考价值。

文件中有如下数据:
1 张三 2 33
2 张三 2 55
3张三 2 66
4 张三 2 77
想对第三行中的数据77修改,其他的都不变怎么做
我现在把第一个和最后一个数据放入map中,根据Key值找到然后修改,可是修改后文件中的数据总是不改变是怎么回事。在线等答案。。。
我就想知道我是可以只对单行的数据修改,还是必须全部读出来,修改某一个后在全部写进去

可以通过BufferedReader 流的形式进行流读取,之后通过readLine方法获取到的内容,之后通过if判断来实现在某些特定位置的查找、增加、删除、修改操作。
举例:
BufferedReader bre = null;
OutputStreamWriter pw = null;//定义一个流
try
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环

if(str.indexOf("排除")<0)//判断是否需要舍弃
pw.write(str);//将要写入文件的内容,可以多次write


bre.close();//关闭流
pw.close();//关闭流
解释:以上方法是实现的删除,if中的条件改变下,即可实现其余的功能。
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
参考技术A 无论你要做什么样的修改,都必须知道文件中的所有数据(在文件末尾追加内容除外),比如你要修改某个值,先取出文件中的所有数据,然后做修改,之后将全部数据写入文件。 参考技术B 把你操作操作文件写入文件的代码贴出来 ,还是没看到你修改值了,我读出来accountid是方法的参数,输入随便的一个id 获得他的blance。这个方法只能看到这些内容 。全部拿出来,挑你想修改的部分修改之后,再将数据完全覆盖到源文件上。但是如果在数据库中操作不必这样,直接update table set 字段=‘a’ where 就可以指定字段更新追问

我也能从文件中取值然后操作,关键是现在想要在把它写进去。现在要么是作为一条新的数据添加了进去,要么就是把文件中所有的数据都覆盖了,我就想覆盖他对应的那一条数据,怎么做?比如我传个accountid是3,然后想把后面的66修改在保存文件中

追答

抱歉,目前io包没发现这样的类与方法。只可以追加 不可以随意位置替换。只能内存整理好数据完全覆盖。你说的这种需求不存在,这种情况下都是采用xml实现的,既然你想模仿数据库,xml应该是你的选择,而不是txt

追问

我读出来的一行数据是字符串格式,循环保存到一个set集合中,怎么在给全部添加进去文件中啊

追答

为什么要保存到set中,保存到String中统一写到文件中,不明白你再想什么

参考技术C 你是修改本地文件的数据。还是修改JAVA中MAP的数据?追问

修改本地文件中的数据,因为要模拟数据库操作

追答

public String read(String filePath)
BufferedReader br = null;
String line = null;
StringBuffer buf = new StringBuffer(); try // 根据文件路径创建缓冲输入流br = new BufferedReader(new FileReader(filePath)); // 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中 while ((line = br.readLine()) != null) // 此处根据实际需要修改某些行的内容if (line.startsWith("a")) buf.append(line).append(" start with a"); else if (line.startsWith("b"))
buf.append(line).append(" start with b");// 如果不用修改, 则按原来的内容回写else buf.append(line);buf.append(System.getProperty("line.separator")); catch (Exception e) e.printStackTrace(); finally // 关闭流if (br != null) try br.close(); catch (IOException e) br = null; return buf.toString();
/**
* 将内容回写到文件中
*/
public void write(String filePath, String content)
BufferedWriter bw = null;try // 根据文件路径创建缓冲输出流
bw = new BufferedWriter(new FileWriter(filePath)); // 将内容写入文件中bw.write(content);
catch (Exception e) e.printStackTrace(); finally // 关闭流if (bw != null)
try bw.close(); catch (IOException e) bw = null;
/**
* 主方法
*/
public static void main(String[] args) String filePath = "D:/try.txt"; // 文件路径
updateFile obj = new updateFile();obj.write(filePath, obj.read(filePath)); // 读取修改文件

字符太少。只能编成这样了。自己调整下格式啊、这个例子你自己还要根据你的实际情况做稍微的改动。就OK了。希望能帮上你

追问

我读出来的一行数据是字符串格式,循环保存到一个set集合中,怎么在给全部添加进去文件中啊

追答

那就从SET中取出来再取做流处理。

本回答被提问者采纳
参考技术D jhvvmvmvmv555555

java:Oracle(table的增删改查,data的增删改查)

1、oracle命名规范:和Java的命名规范很像
    1.严格区分大小写
    2.所有的sql语句都要以‘;‘结尾
    3.所有的sql 都要使用空格区分:sqlplus空格/空格as空格sysdba回车
    4.无论是表名还是列名都必须以字母开头 java中的class必须要以大写字母开头,遵循驼峰命名方式
    5.不能使用ORACLE的保留字
    6.不能与用户定义的其它对象重名
    7.数字,$#_,字母(只能包含A-Z,a-z,0-9,_,$,#)字母加上_
   
emp表中有一列叫emp_no(员工的编号)
2、连接oracle有两种方式:
    清理命令行:cls
    oracle的清理命令行:host cls
     1.PL/SQL连接
   2.原生命令行连接(CMD) sqlplus回车 请输入用户名:scoot 请输入密码(输入的密码在命令行上是不可见):回车 user scott locked!(用户被锁了,无法登陆!)

     加锁命令: 首先要登陆管理员用户 sqlplus / as sysdba:使用超级(系统)管理员登录 alter user oracle的用户名 account locked sysdbs:system database administator(超级管理员) 解锁命令: 首先要登陆管理员用户 sqlplus / as sysdba alter user oracle的用户名 account unlock 查看当前用户登录sql: show user; 修改用户密码:(修改的是密码!) 首先要登陆管理员用户 在oracle中,以后只要看到account这个单词,就一定是用来修改用户的某些信息 但是只需要修改密码的时候就不需要account关键字 alter user oracle的用户名 identified by 123; 解锁并修改用户密码: oracle有自己的错误代码ORA-00922:选项缺失或无效 alter user scott account unlock identified by 新密码; 查看当前用户状态: select username,account_status from dba_status where username=‘scott‘; 在登录oracle完毕后,如何切换用户: conn 用户名;(适用于黑框)
3.oracle的table(表): 数据库是需要存储数据,数据库中的表把存储的数据进行按规定存放。 雇员(emp):存放员工的信息 部门dept:存放部门的信息 table:表名,列,定义列的数据类型,行
  
  列的类型: 最常用的三个类型: 1.varchar2 2.number 3.date/timestap char和varchar2都是字符串类型: char是固定长度的字符串。char(10),这个里面就必须要有10个char类型字符 比如:男,女;省份(河南省) varchar2是不固定长度的字符串。 推荐使用varchar2 CLOB类型(String): 字符集据:很长很长很长很长的字符串,最大可以存储4个G的数据。 decimal(其实就是number的一个长类型:比number类型存储的数据量要大):decimal(100,3):100为多少位整数,3位小数 表数据的唯一标识: 主键:是为了约束每一行数据的唯一性。 遵循特点: 不为空,且唯一。 建表的时候添加主键: create table 表名 (列名 列的类型 primary key not null); 表建立完成以后,指定列为主键 前提是:没有null的数据&&没有重复的数据。 alter table 表名 add constraint(约束,限制) PK_表名_指定的列名 primary key(指定的主键那一列); 表的建立(表名,列名,列的类型): create table 需要创建的表名 (列名和列的类型,列名和列的类型); 创建一个学生表: create table stus (stu_no number(5), stu_name varchar2(20),........); 建立表的同时添加主键: create table 表名(列名 列的类型 primary key not null, 列名 列的类型, 列名 列的类型...); 表的删除: drop table 需要删除的表名; 只清空表中的数据,并不删除表: truncate table 表名; 表的修改: 修改表名: alter table 修改前的表的名字 rename to 新的表名 修改列名: alter table 表名 reanme column 修改的列名 to 新的列名 添加列: alter table 表名 add(需要添加的列名 列的类型); 删除列名: alter table 表名 drop column 需要删除的列名; 修改列的类型: alter table 表名 modify (需要修改的列名 修改的列的类型);
   数据的修改:

       1.增加数据
        insert into 表名 (列名) values (列名对应的值);
      2.删除数据
        delete from teacher where name=‘hahaha‘;
        delete from teacher where id=8;
      3.修改数据
        update teacher set age=29 where id=7;
      4. 通过where关键字进行过滤查询数据
        select * from teacher where id =1;

  视图:
     1.创建
       
create view 视图名 as (select * from 哪个表)(可以将查询得到的表创建成一个视图)

      2.创建替换更名设置权限只读
        create  or replace  view 视图名 as(select st.id  from stu2 st) with read only  
     3.删除
       
drop view 视图名
     4.增加数据
       
insert into 视图名 (列名) values (列名对应的值);  
      5.修改数据

        update 视图名 s set s.AGE=99,s.SALARY=100,s.NAME=‘张三‘ where s.ID=7
      6.查询
        select * from 视图名
        
4.PL/SQL: PL/SQL是一种sql语言 PL/SQL是一个oracle的连接工具 users:包含了所有oracle的用户(在sysdba用户登录模式下,来完成用户名,登录密码,权限配置) tables:包含了当前用户的所有表 简单的查询语句: select * from 表名; 查询出表中的所有数据 oracle中的注释: --
     
data:

1.通过sql语句插入一条数据
  --
insert into 表名 (列名) values (列名对应的值);   


  -- 在使用insert into语句时,如果是字符串类型的数据,就必须要加上单引号‘‘---->中文单引号‘’---->英文单引号‘‘   -- 在使用insert into语句时,如果设定了非空字段,就一定要插入值,否则会报错。   -- 并且insert into teacher(列名) values(列所对应的值);--->要一一对应   insert into teacher (id,name,age,description) values(5,‘tianqi‘,36,‘我是田七‘);   insert into teacher (id, name, age) values(6,‘赵八‘, 34);   --insert into teacher(id,name,description) values(7, ‘王九‘, ‘我是王九‘);   insert into teacher (name, id, age, description) values(‘hahaha‘, 7, 30, ‘我是一个哈哈哈‘);

2.通过sql进行删除数据   -- 在删除的过程中,尽量要使用唯一键进行删除,或者使用主键进行删除   delete from teacher where name=‘hahaha‘;   delete from teacher where id=8; 3.通过sql语句修改一条数据   -- modify/update   -- modify在oracle中适用于列的类型修改,也就是对表的修改进行操作   -- update关键字在oracle中适用于对数据的修改   -- 使用update关键字修改数据时,一定要用唯一键或者主键进行修改   -- 在修改数据时,如果修改多个数据,中间用逗号隔开,字符串带上单引号   update teacher set id=7, name=‘我是哈哈哈‘, age=35, description=‘我是测试数据‘ where id=10;   update teacher set age=29 where id=7;   --where要讲 in, =, <, >, between and, !=(<>), >=, <=, or, is null, is not null, not in, like "_L%", like "%_a%" escape ‘a‘ 4.通过where关键字进行过滤查询    =关键符号   select * from teacher where id =1;    大于和小于   select * from teacher where id > 1;   select * from teacher where id < 9;   大于等于和小于等于   select * from teacher where id >= 5;   select * from teacher where id <= 1;   实现区间(范围)查询   select * from teacher where id >= 2 and id <= 10;   between and 是包含两边的极限数据(闭区间)   select * from teacher where id between 2 and 10;   !=(不等于)   select * from teacher where id != 5;   select * from teacher where id <> 5;   or关键字(或者)   select * from teacher where id =1 or id = 2;   and关键字(并且)   select * from teacher where id = 6 and name = ‘赵八‘;   in关键字(主要的作用:批量操作)   select * from teacher where id in(1, 5, 10);  
 is null 过滤出来所有为null的数据   select * from teacher where description is null;   is not null 过滤出来所有不为null的数据   select * from teacher where description is not null;   not in(过滤出不在范围内的所有数据)   select id from teacher where id > 5只查询出了指定的id   select * from teacher where id not in(select id from teacher where id > 5);   select * from teacher where id not in(1,5,7);   select * from teacher where name not in(‘zhangsan‘, ‘lisi‘, ‘wangwu‘);   模糊查询   --like一般情况下要和%结合起来用,%其实一个占位符,如果把%写在前面,匹配以赵结尾的所有名字,反之匹配以赵开头的所有名字     如果所需要查询的字段前后都加上%,只要包含该查询字段就全部查找出来     select * from teacher where name like ‘%a%‘;   

    以_a%进行模糊查询的时候,会匹配以第二位为‘a‘的字符串     --_就是代表了任意一个字符     select * from teacher where name like ‘_a%‘;     -- 查询以_开头的所有数据     -- 需要把a给去掉     -- 匹配规则:使用escape的时候会,如果_写在需要查询字段的前面,oracle会自动把_转移为任意一个字符     -- 只有把下划线写在需要查询字段的后面,才能使用escape关键字去掉多余字段,只保留下划线。     如果‘%_x%‘,使用escape关键字时,会连同下划线一起去除。     select * from teacher where name like ‘%x_d%‘ escape ‘x‘;     

    *查询     -- 在sql中,可以使用简单的运算符(+,-,*,/)     -- 查询某张表的所有数据     select * from teacher;     -- 查询某张表的指定数据     select name, age from teacher;     -- 查询某张表的指定数据(计算)     select name, age from teacher where id=1;     -- 十年后张三多大?     select name, age+10 from teacher where id=1;     

    给age字段起别名     -- 给字段起别名:别名是为了更好对字段进行描述,也就是说起一个别名并不能随意起,要适用于这个字段的意思     select name, age+10 as age from teacher where id=1;     -- 通常情况下,这里的as关键字可以省略     select name, age+10 age from teacher where id=1;     -- 也可以给表起一个别名(小名)     select t.name from teacher t;          
    更改了列名,里面加了个空格
    
      如果别名中间出现了空格,就会报错(找不到from关键字)
    
    只需要给别名加一个双引号(必须要是双引号)
    select name, age+10 "zhangsan age" from teacher where id=1;
    
    更改了列名,数据内容
    -- 姓名:zhangsan 年龄:33<---\    -- 姓名:也需要加上引号(一定要使用单引号)     select ‘插入数据中的字段‘|| name ‘自己起的列名‘ from teacher;     查询出所有的老师信息,并且以年龄从小到大排列-->order by     -- order by 默认是升序--->asc升序,如果以升序进行排列通常情况下asc省略     select * from teacher order by age asc;     查询出所有的老师信息,并且以年龄从大到小排序---->desc降序     select * from teacher order by age desc;     -- 如果两个老师年纪相同,就以姓名的首字母排序     select * from teacher order by age desc, name;     去重(关键字:distinct)     select distinct age from teacher;     select distinct t.age, t.name from teacher t;     select distinct name from teacher;多表查询     dual,计算1+1等于几?     select 1+1 from dual;-- dual其实也是一张表,这一表是虚拟的,没有列和数据,当查询的时候会进行赋予列和数据,用于方便用户测试使用     -- 查询当前oracle登录的用户     select user from dual;     --sysdate,sysdate-,months_netween(,),add_months(,),next_day(,‘星期几‘),last_day,to_day 5.时间函数     获取当前的时间:sysdate     select sysdate from dual;     sysdate-时间,查询的是指定日期距离当前日期的天数     select t.hire_date, t.name, sysdate, sysdate-hire_date from teacher t;     add_months:对指定日期月份进行加减计算     select add_months(sysdate,3) from dual;     select add_months(sysdate,-3) from dual;     months_between:指定日期距离当前日期的月份     select t.hire_date, sysdate, months_between(sysdate, hire_date) from teacher t;     next_day:查询出指定日期和指定星期几的日期:只能往后不能往前     select next_day(sysdate, ‘星期一‘) from dual;     last_day:查询出当月的最后一天     select last_day(sysdate) from dual;     select t.hire_date, last_day(hire_date) from teacher t;     


    to_date:时间转换函数     -- 默认转换为年月日,不带时分秒     --但是oracle查询当前日期会自带时分秒     -- 使用to_date函数进行转换的时候,查询出的数据仍然遵循oracle的日期规范     -- to_date:只能把年月日时分秒转换为年月日,或者把年月日转换为时分秒,不能转换oracle的日期格式规范     -- oracle中没有任何一个函数可以修改oracle日期格式规范     select to_date(‘1987/08/21 19:18:02‘, ‘yyyy-mm-dd hh24:MI:ss‘) from dual;     --lower(), upper(), initcat(), length(), replace(替换的列名,‘z‘,‘Z‘)(把小z替换成大Z),substr(需要截取的列名,开始截取的位置,最后截取的位置), concat(,),ceil,float,round,trunc    

    to_char:把日期转化为字符串
      提取年
      select to_char(sysdate,‘yyyy‘)from dual
      提取月
      select to_char(sysdate,‘mm‘)from dual
      提取日

       select to_char(sysdate,‘dd‘)from dual

       提取小时(hh是12制的,hh24是24进制)

       select to_char(sysdate,‘hh‘)from dual 

          提取具体时间
       select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘)from dual

     to_number:把字符串转为数字类型 (必须为数字类型的字符串)

       select to_number(‘123456‘)from dual 

     count(*)或count(任意数字):计算数据的数量,其实就是把数据全转化为括号内的数字,再进行统计,查询      出来是多出一个count(?)列。

     实际开发中一定要使用count(1)不要使用count(*)!

      

6.oracle中的函数

    -- lower
    -- 把所有教师名字(英文)全部转换为小写
    select lower(name) from teacher;
    -- upper
    -- 把所有教师名字(英文)全部转换为大写
    select upper(name) from teacher;
    --initcap:首字母开头大写
    select initcap(‘oracle‘) from dual;
    select initcap(name) from teacher;

    --length:获取字符串的长度
    select length(name) from teacher where id =1;

    --replace:有三个参数
    -- 第一个参数:列名(字段);第二个参数:需要替换字段中的某一个字符;第三参数:替换的字符
    select * from teacher where id =1;
    select replace(name,‘z‘,‘Q‘) from teacher where id =1;

    -- substr:和Java的subString差不多
    -- substr有两个参数:第一个参数:列名,第二个参数:从哪一位开始截取(包含最后一位截取的数):name,要截取前两位:subtr(‘name‘,2);--->得到的是ame
    select substr(name, 3) from teacher where id =1;
    -- 如果第二个参数为负数的话,就倒着从最后一位开始往前截取
    select substr(‘zhangsan‘, -3) from dual;
    --substr:
    select * from teacher;
    --subtr:有三个参数:第一个参数:列名,第二个参数:从哪一位开始截取(闭区间),第三个参数:截取到多少位(会从第二个参数的截取位置往后开始进行截取)
    -- oracle中,截取的位置会从0或者1起始(0和1的位置是一样的)
    select substr(‘zhangsan‘,0,5) from teacher where id = 1;
    select substr(‘zhangsan‘,1,5) from teacher where id = 1;

    -- ceil:向上取整
    select ceil(‘12345.1‘) from dual;
    -- floor:向下取整
    select floor(‘12345.1‘) from dual;
    -- round:四舍五入
    select round(‘12345.1‘) from dual;
    select round(‘12345.7‘) from dual;
    select round(‘12845.6‘, -3) from dual;

    -- trunc:截断:会取整数
    -- 第二个参数:保留的小数位
    -- 如果没有小数,并且第二个参数为正数:就原样显示,不会加上.00
    select trunc(‘123444.87978787‘,2) from dual;
    -- 如果没有小数,并且第二个参数为负数:会把整数位从后往前写为0
    select trunc(‘123456‘, -3) from dual;

    -- concat:拼接字符串--->代替||
    select concat(‘zhang‘,‘san‘) from dual;
    select concat(‘姓名是:‘,name) from teacher;








































































以上是关于java io流对文件的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

java io 简单实现对数据的增删改查

django配置模型增删改查

MySQL基本操作--库表增删改查

MySQL-增删改查简易操作

MySQL-增删改查简易操作

库表曾删改查和存储引擎