logo
Tags down

shadow

How to read a block of n lines of a very big text file in both directions, using Java


By : FilGardFoil
Date : October 15 2020, 11:12 AM
To fix this issue If your file doesn't have strict structure (i.e. fixed number of characters per line or per some logical block as Martijn noticed) then you cannot use RandomAccessFile advantages.
In that case you can try to split you huge file into smaller files with fixed number of lines in each (1000 for example). In this case when user wants to see lines from 1800 to 2100 you need to read only 2 files (from several thousands probably if you say that the total size is about 400 Mb).
code :


Share : facebook icon twitter icon

Android/Java: Import text file and read out random lines from within the text file


By : user3580070
Date : March 29 2020, 07:55 AM
should help you out Have you considered using an XML-file for your questions? From the information you provided, the structure should be like this:
code :
<questions>
   <question>
      <id>1</id>
      <text>This is question nr. 1</text>
   </question>
   <question>
      <id>2</id>
      <text>This is question nr. 2</text>
   </question>
   <question>
      <id>3</id>
      <text>This is question nr. 3</text>
   </question>
</questions>

(Java) how to read a text file block by block


By : Tsad
Date : March 29 2020, 07:55 AM
will help you Since data (rows) of each block is not the same you can do something like this. Using \n\n as delimiter for each block and \n for each line
code :
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {   
        BufferedReader br = new BufferedReader(new FileReader("file.txt"));
        StringBuffer sb = new StringBuffer();
        while (true) {
            String line = br.readLine();
            if (line == null) break;
            sb.append(line).append("\n");
        }       

        String[] blocks = sb.toString().split("\n\n");

        for (String block : blocks) {
            block = block.trim();
            // block - individual block from file
            String[] data = block.split("\n");
            for (String d : data) {
                // d - individual line of block 
            }   
        }   
    }    
}

Java read text file in lines, but seperate words in lines into array


By : Sargis Ohanyan
Date : March 29 2020, 07:55 AM
wish help you to fix your issue If you want to have an array of strings per line, then write like this instead:
code :
List<String[]> listwords = new ArrayList<>();

while (in.hasNext()) {
    listwords.add(in.nextLine().split(" "));
}
List<List<String>> listwords = new ArrayList<>();

while (in.hasNext()) {
    listwords.add(Arrays.asList(in.nextLine().split(" ")));
}

Read block of lines in text file


By : BigDutch
Date : March 29 2020, 07:55 AM
it fixes the issue What you can do is to use Regex to try to match each line of the log. If you have match then you create entry otherwise you append the line to the existing entry. I will provide the code to try to explain approach...
code :
//strings for simplification, in your code you should use DateTime and parse properly
public class Entry
{
    public string Timestamp { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
}
//you should define all types you expect in log, I just put info and error
string LogLineRegex = @"(?<date>\d{4}\d{2}\d{2} \d{2}:\d{2}:\d{2}.\d{3}) (?<type>info|error|warning) (?<text>.*)";
Entry rollingEntry = null;

foreach (var line in log)
{
    var match = Regex.Match(line, LogLineRegex);

    if (match.Success)
    {
        if (rollingEntry != null) { entries.Add(rollingEntry); }

        rollingEntry = new Entry{ 
            Timestamp = match.Groups["date"].ToString(),
            Type = match.Groups["type"].ToString(),
            Description = match.Groups["text"].ToString() };
    }
    else
    {
        if (rollingEntry != null) { rollingEntry.Description += $"{Environment.NewLine}{line}"; }
    }
}

Text Processing - Two text files: read block lines from one file and append it after a string in another text file


By : Aydar Djapenov
Date : March 29 2020, 07:55 AM
may help you . Here is an awk script with more extensive test samples.
input.1.txt
code :
bridge-domain AAAA
mac
aging
time 3
!
limit
maximum 12
notification both
!
port-down flush disable
! -1
igmp snooping profile igmp-snoop  1
! 1.1
bridge-domain BBBB
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -2
igmp snooping profile igmp-snoop 2
! 2.1
bridge-domain CCC
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -3
igmp snooping profile igmp-snoop 3
! 3.1
interface Bundle-Ether AAAAA
igmp snooping profile igmp-snoop
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! AAAA section end
igmp snooping profile igmp-snoop
interface Bundle-Ether BBBB
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! BBBB section end
igmp snooping profile igmp-snoop
interface Bundle-Ether CCCC
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! CCCC section end
FNR == NR {    # read insertion paragraph from file 1
    inpSectn = inpSectn $0; # accumlate input lines in inpSectn
    if (NR % 6 == 0) {  # if 6th line add section to array
        sectnArr[++arrCount] = inpSectn; # add inpSectn to ordered array
        inpSectn = "";  # reset inpSectn
    }
    next;      # skip further processing till all file 1 is consumed.
}
1              # output current input line.
FNR % 12 == 0 {   # every 12th line in file 2
    print sectnArr[++arrIdx]; # output section
}
awk -f script.awk input.2.txt input.1.txt
bridge-domain AAAA
mac
aging
time 3
!
limit
maximum 12
notification both
!
port-down flush disable
! -1
igmp snooping profile igmp-snoop  1
interface Bundle-Ether AAAAA
igmp snooping profile igmp-snoop
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! AAAA section end
! 1.1
bridge-domain BBBB
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -2
igmp snooping profile igmp-snoop 2
igmp snooping profile igmp-snoop
interface Bundle-Ether BBBB
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! BBBB section end
! 2.1
bridge-domain CCC
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -3
igmp snooping profile igmp-snoop 3
igmp snooping profile igmp-snoop
interface Bundle-Ether CCCC
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! CCCC section end
! 3.1
Related Posts Related Posts :
  • GWT DatePicker Locale
  • How would you design a twitter like message system, how to design the messaging system?
  • Is there a robust java.util.logging handler implementation of syslog?
  • Aligning messageformat on printing a JTable
  • SXXP0003: Error reported by XML parser: Content is not allowed in prolog
  • ehcache LRU evicting recently used entries
  • java cosine similarity problem
  • Efficient way to implement a client multiple file upload service
  • Regarding Object Comparison
  • How to embed IE in Swing application?
  • Using javax.tools.ToolProvider from a custom classloader?
  • What is the difference between i++ & ++i in a for loop?
  • Match a java main class with regexp
  • Confluence Persistence
  • Is there a way for a SecurityManager in java to selectively grant ReflectPermission("suppressAccessChecks")?
  • Using the camera activity in Android
  • Is communication in java rmi secure?
  • Java - how to make "runnable" JTable?
  • How can I write a Java program to draw a box, an oval, and an arrow?
  • What is the difference between ServletRequest's getRemoteHost() and getServerName()
  • java sparse matrix problem
  • ClassCastException when using HQL
  • how to generate a csv file through java code
  • Netbeans + tomcat on Ubuntu
  • How to connect to a remote MySQL database with Java?
  • System Event Handling using Java
  • How to change Ant script with EMMA code-coverage so it can find runtime coverage data?
  • Java bytecode iconst_0 iadd sequence
  • get the size of a list in a property
  • I want to convert a resultset to a string. I have tried everything but it always gives no data found. Please provide som
  • How can i sort java JTable with an empty Row and force the Empty row always be last?
  • How can I mount network drive in Mac OS X on Java?
  • XStream: How do I map xml mixed attributes and elements to POJOs?
  • begin with java voip
  • java: Preferences API vs. Apache Commons Configuration
  • Howto distinguish Application Quit from System Shutdown
  • How to go about with real GUI development with Java Swing and MVC
  • VB support in Java 6?
  • How to use jar files without package information?
  • how to inherit Constructor from super class to sub class
  • Multidimensional arrays in Java extends which class?
  • Connect to ibm mq with jms . Specify the channel and queue manager
  • Accessing the implicit objects from a JSP EL function
  • Find current heap size with jmap
  • Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||
  • What is the best way to manage configuration data
  • commons fileUpload: specifying an upload directory within Webapplication context
  • ModelAttributes not accessible on jsp
  • Session management between thick client and server?
  • Oracle JDBC intermittent Connection Issue
  • No mapping for LONGVARCHAR in Hibernate 3.2
  • A Packet Blocker Software
  • Anyone using Django in the "Enterprise"
  • Memory leak during repeated lucene query searches?
  • First steps developing in java applet. Directions
  • java efficient de-duplication
  • Converting sequential Java to concurrent java code
  • Java variable number or arguments for a method
  • when developing locally, does it make a difference if the context is '/' or '/appname/'?
  • Problem using JUnit4TestAdapter from eclipse plugin context
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org