如何将文本文件中所有字符的大小写更改为小写?

时间:2017-06-07 03:56:28

标签: java

因此,如果文本文件的内容是FCCCC,则同一文本文件的输出应为fcccc。

我写了以下代码

  import java.lang.*;
  import java.io.*;
  import java.io.FileWriter;
  import java.io.IOException;
  class A
  {
   public static void main(String args[]) throws IOException
   {
      try 
      {
            FileReader reader = new FileReader("doc1.txt");
            FileWriter writer = new FileWriter("doc1.txt", true);
            int character=' ';
            char m;
            while ((character = reader.read()) != -1) 
            {
                m=Character.toLowerCase((char)character);
                System.out.println(m);
                writer.write(m);
            }
            reader.close();
            writer.close();
      } 
      catch (IOException e) 
      {
        e.printStackTrace();
      }
    }
 }

在上面的代码中,如果文本文件的内容是FCCCC,则输出为FCCCCfcccc。

代码中的错误是什么?

4 个答案:

答案 0 :(得分:2)

您的writer正在写入reader正在读取的同一文本文件。

尝试写入其他文本文件。

答案 1 :(得分:0)

试试这个:

import java.io.*;

public class TextFile {
    public static void main (String[] args) throws IOException {
            File file1 = new File("intext.txt");
            File file2 = new File("outtext.txt"); 
            char CharCounter = 0;       
            BufferedReader in = (new BufferedReader(newFileReader(file1)));
            PrintWriter out = (new PrintWriter(new FileWriter(file2)));

            int ch;
            while ((ch = in.read()) != -1){

                if (Character.isUpperCase(ch)){
                    Character.toLowerCase(ch);

                }
                out.write(ch);


            }
            in.close();
            out.close();
        }       
    }

并删除旧文本并重命名新文本。

答案 2 :(得分:0)

您可以使用public static void main(String args[]) throws IOException { RandomAccessFile raf = null; try { raf = new RandomAccessFile("C:\\Untitled.txt", "rw"); long length = raf.length(); byte[] buffer = new byte[(int) length]; long pointer = 0; while (raf.read(buffer) != -1) { String upperCase = new String(buffer).toUpperCase(); raf.seek(pointer); raf.writeBytes(upperCase); pointer = raf.getFilePointer(); } } catch (IOException e) { e.printStackTrace(); } finally { if (raf != null) raf.close(); } } 。它允许您读取和写入文件并获取文件指针和设置文件指针。该计划:

raf.length()

raf.read(buffer)返回文件

中的字节数

buffer.lengthbuffer个字节读入raf.seek(pointer);数组

raf.getFilePointer();将文件指针设置为当前读取之前的位置

CREATE PROCEDURE [dbo].[MIC_UpdateIdeaInline] ( @UpdatedBy int, @UpdatedOn Datetime, @UpdateRecords [UpdateIdeaInline] READONLY ) as BEGIN UPDATE MI SET MI.IsPortalShare= CASE WHEN ColoumnName = 'IsPortalShare' THEN CAST(ur.ColValue as BIT) ELSE IsPortalShare END ,MI.Validity= CASE WHEN ColoumnName = 'Validity' THEN CAST(ur.ColValue as BIT) ELSE Validity END ,MI.DeadLine= CASE WHEN ColoumnName = 'DeadLine' THEN CAST(ur.ColValue as datetime) ELSE DeadLine END ,MI.UpdatedBy=@UpdatedBy, MI.UpdatedOn=@UpdatedOn from [MIC_Idea] MI Join @UpdateRecords ur ON MI.Id=ur.IdeaId SELECT 1 END 在下次读取之前获取文件指针位置,以便能够从此位置开始写入。

答案 3 :(得分:0)

我搜索我的问题来解决我的问题并查看您的代码。 这是我的代码,在控制台中给你你想要的东西。

希望你喜欢。

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;



public class WordCounter {
    private static String Say;

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("Top250imdbMovies.txt");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()){
           String line = scanner.nextLine();
           Say =  line.toLowerCase();
           System.out.println(Say);
         }
       }
    }