文件存入和读取

文件存入和读取

内存与外存读取

package com.bw.a622lx;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {
    private String url="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1141259048,554497535&fm=26&gp=0.jpg";
    private Button txt1;
    private ImageView img1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt1 = (Button) findViewById(R.id.txt1);
        img1 = (ImageView) findViewById(R.id.img1);
        saveDate();
        readDate();
        saveFile();
        readFile();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DownLoadPic();
            }
        }).start();

    }

    public void saveDate(){
        //通过getSharedPreferences获取sharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("1804AFile",MODE_PRIVATE);
        //通过sharedPreferences对象.edit获取对象
        SharedPreferences.Editor editor=sharedPreferences.edit();
        //通过put方法存值
        editor.putString("String","哈哈");
        editor.putBoolean("bool",true);
        //new  hashset  Strings.add方法赋值
        Set<String> strings=new HashSet<>();
        strings.add("嘻嘻");
        strings.add("哈哈");
        strings.add("嘻嘻哈哈");
        //赋值键值对 后面放入set对象
        editor.putStringSet("strings",strings);
        editor.commit();

    }

    public void readDate(){
        SharedPreferences sharedPreferences = getSharedPreferences("1804AFile",MODE_PRIVATE);
        //sharedPreferences.get方法获取文件的东西
        String str=sharedPreferences.getString("String",null);
        Set<String> sets=sharedPreferences.getStringSet("strings",null);
        Log.i("---",str);
        //Foreach 类型:类名  集合
        for (String s:sets){
            Log.i("---",s);
        }

    }

    public void saveFile(){
        //判断sd卡是否可用
        if (Environment.getExternalStorageState().equals
                (Environment.MEDIA_MOUNTED)) {

            try {
                //通过Enviroment.getExternalStorageDirectory.getPath()可以获取到路径
                String sdpath=Environment.getExternalStorageDirectory().getPath();
                //Environment.getExternalStorageDirectory()获取路径
                File file = Environment.getExternalStorageDirectory();
                //利用io流FileoutputStream写入内容
                FileOutputStream fileOutputStream=new FileOutputStream(sdpath+"/a.txt");
                fileOutputStream.write("这就是对了".getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public void readFile(){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File file = new File(Environment.getExternalStorageDirectory().getPath() + "/a.txt");
            try {
                //读取文件
                FileInputStream fileInputStream=new FileInputStream(file);
                byte[] bytes=new byte[1024];
                int len=0;
                //字符串拼接
                StringBuffer buffer=new StringBuffer();
                while((len=fileInputStream.read(bytes))!=-1){
                    //拼接
                    buffer.append(new String(bytes,0,len));
                }

                Log.i("---","这是读出来的"+buffer.toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void DownLoadPic(){
        try {
            //下载文件
            URL url = new URL(this.url);
            //打开连接
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            //设置请求方式方法
            connection.setRequestMethod("GET");
            //是否连接
            connection.connect();
            if (connection.getResponseCode()==200){
                InputStream inputStream = connection.getInputStream();
                int len=0;
                byte[] bytes=new byte[1024];
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    String path = Environment.getExternalStorageDirectory().getPath();
                    File file=new File(path+"/p.jpg");
                    //输出流
                    FileOutputStream fileOutputStream=new FileOutputStream(file);
                    while((len=inputStream.read(bytes))!=-1){
                        //将读取的图片直接存入
                       fileOutputStream.write(bytes,0,len);
                    }
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readImage(){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            String path = Environment.getExternalStorageDirectory().getPath();
            File file=new File(path+"/p.jpg");
            try {
                FileInputStream inputStream=new FileInputStream(file);
                //通过图片工厂类获取bitmap对象
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                //直接放图片
                img1.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中的链表是一种常用的数据结构,它可以动态地存储和访问数据。在文件操作中,我们可以通过将链表中的数据存入文件中,也可以通过读取文件中的数据来构造链表。下面是一个简单的示例,演示了如何将链表中的数据存入文件读取: ``` #include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *next; } Node; void insert(Node **head, int data) { Node *new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->next = *head; *head = new_node; } void print_list(Node *head) { Node *current = head; while(current != NULL) { printf("%d ", current->data); current = current->next; } } void save_list(Node *head, const char* filename) { FILE *fp = fopen(filename, "w"); Node *current = head; while(current != NULL) { fprintf(fp, "%d ", current->data); current = current->next; } fclose(fp); } void load_list(Node **head, const char* filename) { FILE *fp = fopen(filename, "r"); int data; while(fscanf(fp, "%d ", &data) != EOF) { insert(head, data); } fclose(fp); } int main() { Node *head = NULL; insert(&head, 3); insert(&head, 5); insert(&head, 7); printf("Original list: "); print_list(head); save_list(head, "list.txt"); printf("\nList saved to file.\n"); Node *new_head = NULL; load_list(&new_head, "list.txt"); printf("Loaded list: "); print_list(new_head); return 0; } ``` 在这个示例中,我们定义了一个链表结构体`Node`,包含了一个整数类型的数据`data`和一个指向下一个节点的指针`next`。我们通过`insert`函数向链表中插入新的节点,并通过`print_list`函数打印链表中的所有数据。 在`save_list`函数中,我们打开一个文件,并将链表中的每个节点的数据依次写入文件中。在`load_list`函数中,我们打开这个文件,并逐行读取文件中的数据,然后通过`insert`函数将这些数据插入到一个新的链表中。 在`main`函数中,我们先创建一个链表并向其中插入一些数据,然后调用`save_list`函数将链表中的数据存入一个文件中。接着,我们调用`load_list`函数读取这个文件,并构造出一个新的链表。最后,我们分别打印原始链表和新的链表中的所有数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值